[xsl] Move a section to different part of the XML tree based on child values

Subject: [xsl] Move a section to different part of the XML tree based on child values
From: Rob Newman <rlnewman@xxxxxxxx>
Date: Mon, 21 Jul 2008 16:25:30 -0700
Hi there XML gurus,

I have an XML file with the contents:

output.xml

<Folder>
    <name>Station List</name>
    <Placemark>
       <name>PFO</name>
       <visibility>1</visibility>
    </Placemark>
    <Placemark>
       <name>MONP2</name>
       <visibility>1</visibility>
    </Placemark>
    <Placemark>
       <name>MONP</name>
       <visibility>0</visibility>
    </Placemark>
    <Placemark>
       <name>POTR</name>
       <visibility>0</visibility>
    </Placemark>
</Folder>

Based on the value of the visibility tag, I would like to rearrange the output to be split into two sections - 'Visible Stations' and 'Invisible Stations':

output_reordered.xml

<Folder>
    <Section>
        <name>Visible Stations</name>
        <Placemark>
           <name>PFO</name>
           <visibility>1</visibility>
        </Placemark>
        <Placemark>
           <name>MONP2</name>
           <visibility>1</visibility>
        </Placemark>
    </Section>
    <Section>
        <name>Invisible Stations</name>
        <Placemark>
           <name>MONP</name>
           <visibility>0</visibility>
        </Placemark>
        <Placemark>
           <name>POTR</name>
           <visibility>0</visibility>
        </Placemark>
    </Section>
</Folder>

I have a template that matches on the Folder tag, and I get the value of visibility into a variable.

convert.xsl

<xsl:template match="/">
<Folder>
<!-- How do I enter and populate the Sections here? -->
<xsl:template match="Folder/Placemark">
<xsl:variable name="thisVisible"><xsl:value-of select="visibility" /></xsl:variable>


<!-- Some sort of if/else statement that then moves the Placemark to the correct location? -->

<Placemark>
<name><xsl:value-of select="name" /></name>
<visibility><xsl:value-of select="visibility" /></ visibility>
</Placemark>
</xsl:template>
</Folder>
</xsl:template>


How can I ensure that if the visibility tag is set to 1 it gets inserted into the result tree in the right section (in the Folder named Visible Stations) and if the tag is set to 0 it gets inserted into the result tree in the Invisible Stations tag? I assume I need to create the Sections by hand, but then moving the Placemark sections into one of the Sections is the part I am stuck on.

Thanks in advance,
- Rob

Current Thread