Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

xslt - Can you transform unordered xml to match an xsd:sequence order?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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):

<person>
  <property name="address" value="5" />
  <property name="firstname" value="1234567890" />
  <property name="lastname" value="The BFG" />
</person>

External XSD Schema file (schema.xsd):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <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>
</xs:schema>

XSLT 2.0 Stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="input">
    <xsl:copy-of select="/"/>
  </xsl:variable>

  <xsl:template match="/*">
    <xsl:variable name="firstContext" select="name()"/>
    <xsl:variable name="xsdElems" select="document('schema.xsd')/xs:schema/xs:element[@name=$firstContext]/xs:complexType/xs:sequence/xs:element/@name"/>
    <xsl:element name="{$firstContext}">
      <xsl:for-each select="$xsdElems">
        <xsl:variable name="secondContext" select="."/>
        <xsl:element name="{$secondContext}">
          <xsl:value-of select="$input/*/*[@name=$secondContext]/@value"/>
        </xsl:element>
      </xsl:for-each>      
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

XML Output:

<person>
   <firstname>1234567890</firstname>
   <lastname>The BFG</lastname>
   <address>5</address>
</person>

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...