I have a problem with namespace position in result xml file after xsl transformation.
My transformation stylesheet looks like
<?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>
In output xml file I want to get root node as
<SmartDriveUpdates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LightSpeedXMLSchema.xsd">
But instead I have
<SmartDriveUpdates xsi:noNamespaceSchemaLocation="LightSpeedXMLSchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
I also tried to precode root node in xsl stylesheet as
<SmartDriveUpdates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LightSpeedXMLSchema.xsd">
...
</SmartDriveUpdates>
But I get the same wrong result.
Transformation aplied to xml file by using Transform method from system.xml.xsl.xslcompiledtransform .NET class. I use PowerShell for this purpose:
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 )
}
Can someone help me to resolve this?
Thanks
The order of namespace definitions and attributes is implementation dependent.
You have two choices:
Use another XSLT processor — Saxon 6.5.4 or Saxon 9.x (there is a .NET version), some versions of Altova (XML-SPY) and XQSharp all generate the output as desired.
Continue using XslCompiledTransform, but implement your own XmlWriter object. You have the freedom in the implementation of the WriteElementString Method() to produce the element’s serialization in any way desired.