Hi i need to transform unorderd xml using xslt to the correct order as specified in an xsd schema
<Person>
<property name="address" value="5" />
<property name="firstname" value="1234567890" />
<property name="lastname" value="The BFG" />
</Person>
would need to be transformed using
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The xml could have the properties in any order, with upwars of 10+ property elements.I have tried using an xsl:for-each to try and process the xml but i'm stumped at how to get the xslt to transform the xml into the correct order as defined by the sequence
any help would be appreciated
This may not be the best way, but it seems to work ok. I'm not sure if the order that the
xs:element
's are processed is guaranteed though. Also, this is an XSLT 2.0 answer tested with Saxon-HE 9.3.0.5 in oXygen.XML Input (modified the case of
Person
to match the schema):External XSD Schema file (schema.xsd):
XSLT 2.0 Stylesheet:
XML Output:
Hope this helps.
Here is an XSLT 1.0 solution:
when this transformation is applied on the provided XML document (
Person
renamed toperson
to match the schema):and if the provided XML schema is in the file
c:\temp\delete\schema.xsd
:then the wanted, correct result is produced: