People must get sick of answering questions on sorting, but I haven't found an example on the web that matches my situation (Maybe I am asking the wrong question). My XML file has this sort of structure
All I want to do is sort the pieces within the blocks and output the whole lot. I can sort the blocks, I can sort the pieces and lose the blocks and I can sort the pieces and keep the block tags, but not the block data. My current xsl is
Any help would be greatly appreciated
Code:
<?xml version="1.0"?> <Building> <Block> <Id>Ba2150</Id> <Elevation>140</Elevation> <Piece> <Description>Rustic</Description> <Elevation>180</Elevation> <Qty>1</Qty> </Piece> <Piece> <Description>Rustic</Description> <Elevation>1870</Elevation> <Qty>1</Qty> </Piece> <Piece> <Description>Red</Description> <Elevation>500</Elevation> <Qty>1</Qty> </Piece> <Piece> <Description>Blue</Description> <Elevation>1430</Elevation> <Qty>1</Qty> </Piece> <Piece> <Description>Side</Description> <Elevation>140</Elevation> <Qty>2</Qty> </Piece> <Piece> <Description>Bottom</Description> <Elevation>140</Elevation> <Qty>1</Qty> </Piece> <Piece> <Description>Back</Description> <Elevation>140</Elevation> <Qty>1</Qty> </Piece> <Piece> <Description>Top</Description> <Elevation>2190</Elevation> <Qty>1</Qty> </Piece> </Block> </Building>
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Building">
<xsl:copy>
<xsl:apply-templates select="Block">
<xsl:sort select="Elevation" data-type="number"/> <!-- not really necessary, already sorted -->
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Block">
<xsl:copy>
<xsl:apply-templates select="Piece">
<xsl:sort select="Elevation" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Comment