html - Powershell:将生成的表写入 html 文件
问题描述
我总共有两个文件,
- PowerShell 脚本
- HTML 文件
在脚本的帮助下,我创建了一个 html 表。我想将表格内容设置为 html 文件中的固定位置。
<table class="Class_OPC_Item">
<col style="width: 300px;">
<col style="width: 50px;">
<col style="width: 50px;">
<col style="width: 50px;">
<col style="width: *;">
<!-- $$$OPC-ITEM-ENTRIES START$$$ -->
<!-- $$$OPC-ITEM-ENTRIES END$$$ -->
<a href="#footnote_10">Footnotes</a>
</table>
这是我当前生成和转换我的 html 的代码:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$Choosefile = New-Object System.Windows.Forms.OpenFileDialog
$Choosefile.Filter ="UAR-Files (*.uar)|*.uar"
$Choosefile.ShowDialog()
if($Choosefile.FileName -ne "")
{
#Path to .uar-File
[xml]$uar = Get-Content -Path $ChooseFile.FileName
#Says that we are using namespaces
$ns = New-Object System.Xml.XmlNamespaceManager($uar.NameTable)
#namespaces of XML
$ns=@{asdf="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd";
ua="http://br-automation.com/OpcUa/configuration/NodeSet.xsd";
xsi="http://www.w3.org/2001/XMLSchema-instance";
uax="http://opcfoundation.org/UA/2008/02/Types.xsd";
xsd="http://www.w3.org/2001/XMLSchema";
pv="http://br-automation.com/OpcUa/PLC/PV.xsd"}
#Lookuptable for DataTypes
$LookUpTable=@{
"i=1"="Boolean"
"i=2"="Sbyte"
"i=3"="Byte"
"i=4"="Int16"
"i=5"="UInt16"
"i=6"="Int32"
"i=7"="UInt32"
"i=8"="Int64"
"i=9"="UInt64"
"i=10"="Float"
"i=11"="Double"
"i=12"="String"
"i=13"="DateTime"
"i=15"="ByteString"
"i=21"="LocalizedText"
"i=35"="Organizes"
"i=37"="HasModellingRule"
"i=38"="HasEncoding"
"i=39"="HasDescription"
"i=40"="HasTypeDefinition"
"i=45"="HasSubtype"
"i=46"="HasProperty"
"i=47"="HasComponent"
"i=69"="DataTypeDescriptionType"
"i=72"="DataTypeDictionaryType"
"i=76"="DataTypeEncodingType"
"i=294"="Argument"
"i=296"="UtcTime"
"i=7594"="EnumValueType"
}
#XPATH with namespaces. Only Valid DataTypes and NodeId´s
$result=Select-xml -xml $uar -xpath "//asdf:UAVariable[contains(@NodeId,'ns=1;s=::')][starts-with(@DataType,'i=')]" -namespace $ns | select -ExpandProperty node
#just a loop to replace "ns=1;s=::AsGlobalPV:"
$result | foreach {$_.NodeId = $_.NodeId -replace 'ns=1;s=::AsGlobalPV:'}
#another loop if its not GlobalPV
$result | foreach {$_.NodeId = $_.NodeId -replace 'ns=1;s=::'}
#2nd Array for writing my Valid nodes for html output (FINAL RESULT)
$result2=@()
#loop to check if arrays are Valid
$result | foreach{
$AttExists = $_.Arraydimensions
$NodeExists = $_.References.Reference.ReferenceType
if ($AttExists)
{
if ($NodeExists -eq 'HasComponent')
{
#Nothing we dont want Arraydeclarations
}
else
{
$result2 += $_
}
}
else
{
$result2 += $_
}
}
#loop to split up "Description" and adding new values to array
$result2 | foreach{
$ChildExists=$_.Description
if($ChildExists){
if($ChildExists -match ';')
{
$splitarray=$ChildExists.Split(";")
$_ | Add-Member -MemberType NoteProperty -Name "Scale" -value $splitarray[0]
$_ | Add-Member -MemberType NoteProperty -Name "Unit" -value $splitarray[1]
$_ | Add-Member -MemberType NoteProperty -Name "Comment" -value $splitarray[2]
}
else
{
$_ | Add-Member -MemberType NoteProperty -Name "Scale" -value ""
$_ | Add-Member -MemberType NoteProperty -Name "Unit" -value ""
$_ | Add-Member -MemberType NoteProperty -Name "Comment" -value $ChildExists
}
}
else
{
$_ | Add-Member -MemberType NoteProperty -Name "Scale" -value ""
$_ | Add-Member -MemberType NoteProperty -Name "Unit" -value ""
$_ | Add-Member -MemberType NoteProperty -Name "Comment" -value ""
}
}
#loop for DataTypes translation
$result2 | foreach{
if($LookUpTable.ContainsKey($_.DataType))
{
$_.DataType=$LookUpTable[$_.DataType]
}
}
$SaveFile = New-Object System.Windows.Forms.SaveFileDialog
$SaveFile.Filter = "Html-Files (*.html)|*.html"
$SaveFile.showdialog() | Out-Null
$result2 | ConvertTo-Html `
-Property NodeId, DataType, Scale, Unit, Comment `
> $SaveFile.FileName
此 html 应发布到顶部的 2 个占位符之间的另一个 html 中。这里的任何人都可以帮助我或做过类似的事情并且可以给我一个提示吗?
提前致谢
解决方案
将占位符边界放在不同的行上,您可以编写一个简单的解析器来连接文件:
# Generate our html table
$HTMLTable = Get-HtmlTable # replace with your code
$allLines = switch -File .\path\to\template.html -WildCard {
'*<!-- $$$OPC-ITEM-ENTRIES START$$$ -->*' {
# append the HTML table
$_
$HTMLTable
}
default {
# output the line as-is from the html template file by default
$_
}
}
# Output the whole thing to a new html file
$allLines |Set-Content .\output.html
推荐阅读
- java - JT400 - IBMi 机器拒绝 JDBC 连接,但在 Windows 机器上工作
- r - 将具有 NA 的相似长度向量列表合并为一个向量
- swift - SwiftUI:在选项卡视图中显示键盘在第二个选项卡视图下方添加了一个奇怪的空间
- spring-boot - Spring Boot Kotlin 实体属性 val vs var
- facebook - Facebook 照片上传日期时间戳
- blazor - 如何在剃须刀类库的子布局中指定“MainLayout”
- javascript - 角度选择删除空白抛出 id 未定义错误
- ansible - Ansible 保险库密码文件格式是什么?
- java - 无效的数组显示
- javascript - 根据 span 标签中数字的大小设置显示属性