transformation in particular sequence

删除回忆录丶 提交于 2019-12-12 02:00:06

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/23348290/transformation-in-particular-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!