xsl-list
[Top] [All Lists]

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

2008-07-21 20:57:51
I think, you can achieve the result by following stylesheet fragment,

<xsl:template match="/Folder">
  <Folder>
    <Section>
      <name>Visible Stations</name>
      <xsl:copy-of select="Placemark[visibility = '1']" />
    </Section>
    <Section>
      <name>Invisible Stations</name>
      <xsl:copy-of select="Placemark[visibility = '0']" />
    </Section>
  </Folder>
</xsl:template>

(not tested ...)

On 7/22/08, Rob Newman <rlnewman(_at_)ucsd(_dot_)edu> wrote:
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


-- 
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>