Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Arrange nodes under a closed element - XSLT

$
0
0

So I have this XML:

<Main><TB>
    --> some elements - not relevant<Area><Type>A</Type><Street><Position>5</Position><House>

       --> some elements</House></Street><Street><Position>5</Position><Block>

       --> some elements</Block></Street><Street><Position>6</Position><House>

       --> some elements</House></Street><Street><Position>6</Position><Block>

       --> some elements</Block></Street></Area><Area><Type>B</Type>
   --> same structure but with several repetitions of Position 7 and 8.</Area></TB></Main>

And I want to order it like that:

<Area><Type>A</Type><Street><Position>5</Position><House>

       --> some elements</House><Block>

       --> some elements</Block></Street><Street><Position>6</Position><House>

       --> some elements</House><Block>

       --> some elements</Block></Street></Area><Area><Type>B</Type>
   --> same structure for Position 7 and 8.</Area>

And I am using this XSLT to transform it:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:strip-space elements="*" /><xsl:output method="xml" indent="yes" /><xsl:key name="streetByPosition" match="Street" use="Position" /><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy></xsl:template><!-- for the first Street in each Position --><xsl:template match="Street[generate-id() =
                            generate-id(key('streetByPosition', Position)[1])]"><Street><!-- copy in the Position element once only --><xsl:apply-templates select="Position" /><!-- copy in all sub-elements except Position from all matching Streets--><xsl:apply-templates select="
            key('streetByPosition', Position)/*[not(self::Position)]" /></Street></xsl:template><!-- ignore all other Street elements --><xsl:template match="Street" /></xsl:stylesheet>

The ordering works completely fine. But if I have repetitive Position numbers in different Type then I get all the Houses and Blocks arranged in the first Type where I had the repetitive Position number. For example:

<Area><Type>A</Type><Street><Position>5</Position><House>

           --> some elements</House></Street><Street><Position>5</Position><Block>

           --> some elements</Block></Street>
....<Area><Type>B</Type><Street><Position>5</Position><House>

       --> some elements</House></Street>

Then the elements under Position 5 in Type B will be moved from there to under Position 5 in TypeA. And I don't want that. I want the Houses and Blocks to be arranged but stay in their own types and areas.

Can anyone provide me with solution of how do I have to change my XSLT in order to fix this?

P.S. the names of the XML tags were changed for simplification reasons. And I cannot use xslt-2.0 because my editor doesn't support it.


Viewing all articles
Browse latest Browse all 935

Trending Articles