在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