在xsl轉換後,我在結果xml文件中的名稱空間位置有問題。XSLT:將名稱空間設置爲第一個屬性
我的轉換樣式看起來像
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output indent="yes" method="xml" />
<xsl:template match="/">
<xsl:element name="SmartDriveUpdates">
<xsl:attribute name="xsi:noNamespaceSchemaLocation">
<xsl:text>LightSpeedXMLSchema.xsd</xsl:text>
</xsl:attribute>
...
</xsl:element>
在輸出XML文件我想根節點
而是我有
<SmartDriveUpdates xsi:noNamespaceSchemaLocation="LightSpeedXMLSchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
我也想前置碼根節點XSL樣式表作爲
<SmartDriveUpdates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LightSpeedXMLSchema.xsd">
...
</SmartDriveUpdates>
但我得到同樣的錯誤的結果。
通過使用System.xml.xsl.xslcompiledtransform .NET類的Transform方法轉換爲xml文件。我爲此使用PowerShell:
Function Convert-WithXslt($originalXmlFilePath, $xslFilePath, $outputFilePath) {
## Simplistic error handling
$xslFilePath = Resolve-Path $xslFilePath
If(-not (Test-Path $xslFilePath)) {
Throw "Can't find the XSL file"
}
$originalXmlFilePath = Resolve-Path $originalXmlFilePath
If(-not (Test-Path $originalXmlFilePath)) {
Throw "Can't find the XML file"
}
#$outputFilePath = Resolve-Path $outputFilePath
If(-not (Test-Path (Split-Path $originalXmlFilePath))) {
Throw "Can't find the output folder"
}
## Get an XSL Transform object (try for the new .Net 3.5 version first)
$EAP = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"
$script:xslt = New-Object system.xml.xsl.xslcompiledtransform
Trap [System.Management.Automation.PSArgumentException]
{ # no 3.5, use the slower 2.0 one
$ErrorActionPreference = $EAP
$script:xslt = New-Object system.xml.xsl.xsltransform
}
$ErrorActionPreference = $EAP
## load xslt file
$xslt.load($xslFilePath)
## transform
$xslt.Transform($originalXmlFilePath, $outputFilePath)
}
有人可以幫我解決這個問題嗎?
感謝
問得好,+1的結果。請參閱我的解答和解決方案 – 2011-03-16 12:42:27