Jump to content

Trying to reorder XML elements -- almost there!


tomcon

Recommended Posts

I am thinking this should not be too difficult -- my first foray into XSLT!Want to reorder elements under a root element. The following code ALMOST does it, but there are a few glitches.

  <xsl:template match="RootElement">	<xsl:copy use-attribute-sets="MyAttrSet"/>	<xsl:copy-of select="Element3"/>	<xsl:copy-of select="Element1"/>	<xsl:copy-of select="Element2"/>  </xsl:template>

The above code does correctly re-order the elements in the output as i desire. But it has the following two defects. Can you help me solve them?1. The instances of Element3, Element1, Element2 are not enclosed by the root element. The RootElement is closed, and the other elements follow. The output looks like this. Can you tell me how to make the RootElement be the root element of the output?

<RootElement MyAttr=""/><Element3>  ...</Element3><Element1>  ...</Element1><Element2>  ....</Element2>

2. The attribute value is not copied to the Output. As shown above, the atrribute name is in the output, but the value is "", instead of the value that was in the input. How do i make the value of the attribute in the input also come out in the output?Thanks much!Tom

Link to comment
Share on other sites

You would better show your XML input but I think you simply want e.g.

<xsl:template match="RootElement">   <xsl:copy>	 <xsl:copy-of select="@*"/>	 <xsl:copy-of select="Element3"/>	 <xsl:copy-of select="Element1"/>	 <xsl:copy-of select="Element2"/>  </xsl:copy></xsl:template>

With XSLT 2.0 you can shorten that to

<xsl:template match="RootElement">   <xsl:copy>	 <xsl:copy-of select="@*, Element3, Element1, Element2"/>  </xsl:copy></xsl:template>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...