XSLT: Defining a Namespace as the First Attribute

I have a problem with the position of the namespace in the xml of the result after xsl conversion.

My transform 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 the 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 predict the root node in the 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.

Convert to xml file using the Transform method from the system.xml.xsl.xslcompiledtransform.NET class. For this purpose I use 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 ) } 

Can someone help me solve this?

thanks

+4
source share
3 answers

The order in which namespaces and attributes are defined depends on the implementation .

You have two options:

  • 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 generate the result as desired.

  • Continue to use XslCompiledTransform, but implement your own XmlWriter object. You have the freedom to implement the WriteElementString () method to get the element serialized in any way you want.

+1
source

The order of the attributes and attributes of the namespace declaration does not matter, and I don’t think you can define this order using XSLT. Why is an order question for you?

+2
source

If you don’t want to discard XslCompiledTransform, you can use XQSharp XmlWriter instead of writing your own, which may lead to the result you are looking for,

0
source

Source: https://habr.com/ru/post/1343912/


All Articles