[xsl] Fetch a sequence of nodes from input file

Subject: [xsl] Fetch a sequence of nodes from input file
From: "rowan@xxxxxxxxxxxxxxxxxxxxx" <rowan@xxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 23 Sep 2009 13:05:12 -0400
<xsl:copy-of select="//items/item[name = current()/ref]"/>

This works fine thank you.

Now on to the next problem. Here's a slightly more complicated input file:

<?xml version="1.0" encoding="UTF-8"?>
<items>
   <item>
      <name>First</name>
      <level>0</level>
      <ref/>
      <contents>First contents.</contents>
   </item>
   <item>
      <name>Second</name>
      <level>1</level>
      <ref/>
      <contents>Second contents.</contents>
   </item>
   <item>
      <name>Third</name>
      <level>2</level>
      <ref/>
      <contents>Third contents.</contents>
   </item>
   <item>
      <name>Fourth</name>
      <level>3</level>
      <ref/>
      <contents>Fourth contents.</contents>
   </item>
   <item>
      <name>Fifth</name>
      <level>1</level>
      <ref/>
      <contents>Fifth contents.</contents>
   </item>
   <item>
      <name>Sixth</name>
      <level>2</level>
      <ref>Third</ref>
      <contents>Sixth contents.</contents>
   </item>
   <item>
      <name>Seventh</name>
      <level>1</level>
      <ref/>
      <contents>Seventh contents.</contents>
   </item>
</items>

When I find an item with an empty <ref> element, I want to just copy it to
the putput. But when I find one with a non-empty <ref> element, I want to
copy <item>s in sequence, starting with the one whose <name> matches the
<ref> of the source <item> (I'll call this the start item), and finishing
with the last one whose <level> value is greater than the <level> of the
start item. So I want to stop just before the <item> whose <level> is <=
the <level> of the start item.

What I want to produce from the above file is:

<?xml version="1.0" encoding="UTF-8"?>
<items>
   <item>
      <name>First</name>
      <level>0</level>
      <ref/>
      <contents>First contents.</contents>
   </item>
   <item>
      <name>Second</name>
      <level>1</level>
      <ref/>
      <contents>Second contents.</contents>
   </item>
   <item>
      <name>Third</name>
      <level>2</level>
      <ref/>
      <contents>Third contents.</contents>
   </item>
   <item>
      <name>Fourth</name>
      <level>3</level>
      <ref/>
      <contents>Fourth contents.</contents>
   </item>
   <item>
      <name>Fifth</name>
      <level>1</level>
      <ref/>
      <contents>Fifth contents.</contents>
   </item>
   <item>
      <name>Third</name>
      <level>2</level>
      <ref/>
      <contents>Third contents.</contents>
   </item>
   <item>
      <name>Fourth</name>
      <level>3</level>
      <ref/>
      <contents>Fourth contents.</contents>
   </item>
   <item>
      <name>Seventh</name>
      <level>1</level>
      <ref/>
      <contents>Seventh contents.</contents>
   </item>
</items>

Any hints on the best way to do this?

I've been trying to do it by finding the position() in the full item
nodeset of the first and last items in the subset I want to copy (a bit
like subscripts in an array in a procedural language) but I'm beginning to
think I'm barking up the wrong tree.

Many thanks - Rowan

--------------------------------------------------------------------
mail2web - Check your email from the web at
http://link.mail2web.com/mail2web

Current Thread