Domanda

I would like to know that is it possible to move xml element up or down in element tree.

The input xml.

<ROOT>
 <A1>A</A1>
 <B1>B</B1>
 <C1>C</C1>
 <D1>D</D1>
</ROOT>

The required output xml.

<ROOT>
 <A1>A</A1>
 <D1>D</D1>
 <B1>B</B1>
 <C1>C</C1>
</ROOT>

XSD Sequence Rule

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"      xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="ROOT">
 <xs:complexType>
  <xs:sequence>
    <xs:element type="xs:string" name="A1"/>
    <xs:element type="xs:string" name="D1"/>
    <xs:element type="xs:string" name="B1"/>
    <xs:element type="xs:string" name="C1"/>
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>

Can anyone provide me some XSLT example to do this such of thing? Thank you for any help in advance.

È stato utile?

Soluzione

Martin Honnen help me to get answer from other post, hence just thought to share answer to help others but still I am wondering if there is any other way to do this - as in this approach if there is change in XSD tag sequence one has to change xsl sequence too.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="node()|@*">
<xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="ROOT">
<ROOT>
<xsl:apply-templates select="A1"/>
<xsl:apply-templates select="D1"/>
<xsl:apply-templates select="C1"/>
<xsl:apply-templates select="B1"/>
</ROOT>
</xsl:template> 

<xsl:template match="A1">
<a1>
    <xsl:apply-templates />
</a1>
</xsl:template>

 <xsl:template match="B1">
<b1>
    <xsl:apply-templates />
</b1>
</xsl:template>

 <xsl:template match="C1">
<c1>
    <xsl:apply-templates />
</c1>
</xsl:template>

<xsl:template match="D1">
<d1>
    <xsl:apply-templates />
</d1>
</xsl:template>

 </xsl:stylesheet>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top