0

Given the following structure, how to copy the first and the second nodes with all their elements from the document based on the predicate in XSLT:

<list>
  <slot>xx</slot>
   <data>
       <name>xxx</name>
       <age>xxx</age>  
   </data>      
   <data>
       <name>xxx</name>
       <age>xxx</age>  
   </data> 
   <data>
       <name>xxx</name>
       <age>xxx</age>  
   </data> 
</list> 
<list>
  <slot>xx</slot>
   <data>
       <name>xxx</name>
       <age>xxx</age>  
   </data> 
   <data>
       <name>xxx</name>
       <age>xxx</age>  
   </data> 
   <data>
       <name>xxx</name>
       <age>xxx</age>  
   </data> 
</list> 

How to select the first and the second occurence of data (without the data element itself, only name, age) from the list, where the slot is equal to a different variable, i.e the first list has the slot=02, but I need the data from the second list, where the slot=01. But it does not really matter the order of the list by a slot as long as slot=$slotvariable.

I tried the following statement, but it did not produce any results:

<xsl:element name="{'Lastdata'}">
  <xsl:copy-of select="list/data[position()=1 and slot = $slotvariable]" />
</xsl:element>
<xsl:element name="{'prevdata'}">
  <xsl:copy-of select="list/data[position()=2 and slot = $slotvariable]" />
</xsl:element>

Any working suggestions would be appreciated

1
  • 1
    Things that are missing from this question: 1) An XML sample that actually matches your explanations instead of containing all xxxxx. 2) The output as you imagine it. Please add these things (you can edit your question) so people are not forced to guess in their answers. Commented Mar 20, 2011 at 2:01

2 Answers 2

5

If I understood your question correctly, then:

<Lastdata>
  <xsl:copy-of select="list[slot=$slotvariable]/data[1]/*" />
</Lastdata>
<prevdata>
  <xsl:copy-of select="list[slot=$slotvariable]/data[2]/*" />
<prevdata>

Hints:

  • Don't use <xsl:element> unless you have a dynamic name based on an expression.
  • [1] is a shorthand for [position() = 1]
Sign up to request clarification or add additional context in comments.

Comments

0

The following stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="slot" select="'slot1'"/>
    <xsl:template match="/lists/list">
        <xsl:copy-of select="data[../slot=$slot][position()&lt;3]/*"/>
    </xsl:template>
</xsl:stylesheet>

Applied to this source:

<lists>
    <list>
      <slot>slot1</slot>
       <data>
           <name>George</name>
           <age>7</age>  
       </data> 
       <data>
           <name>Bob</name>
           <age>22</age>  
       </data> 
       <data>
           <name>James</name>
           <age>77</age>  
       </data> 
    </list> 
    <list>
      <slot>slot2</slot>
       <data>
           <name>Wendy</name>
           <age>25</age>  
       </data> 
    </list> 
</lists>

Produces the following result:

<name>George</name>
<age>7</age>
<name>Bob</name>
<age>22</age>

8 Comments

data[../slot[1]=$slot] is equivalent to data[slot=$slot], and I don't think you can use variables in match expressions.
@Tomalak - Yup. I'd already changed that before I saw your comment.
Next time when you write "produces the following output", also check if it actually does. ;-) (There is still a variable in your match expression.)
@Tomalek - What? I did. That's exactly what it outputs in Firefox. The variable causes no problems. I copy/pasted the result.
Check with another tool and you will get an error. Variables are not supported in match expressions in XSLT 1.0. Try here for example: shell-tools.net/index.php?op=xslt
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.