2

I'm having trouble to retrieve all the parents for repeating child name.

<NCAAScores>
   <levels>
      <level>
         <name>Western Conference</name>
         <teams>
            <team>
               <name>Dallas Stars</name>
               <scorable>
                  <win>60</win>
                  <lose>35</lose>
               </scorable>
            </team>
            <team>
               <name>Chicago Blackhawks</name>
               <scorable>
                  <win>60</win>
                  <lose>23</lose>
               </scorable>
            </team>
            <team>
               <name>Edmonton Oilers</name>
               <scorable>
                  <win>55</win>
                  <lose>9</lose>
               </scorable>
            </team>
            <team>
               <name>Philadelphia Flyers</name>
               <scorable>
                  <win>5</win>
                  <lose>9</lose>
               </scorable>
            </team>
         </teams>
      </level>
      <level>
         <name>Eastern Conference</name>
         <teams>
            <team>
               <name>Dallas Stars</name>
               <scorable>
                  <win>1</win>
                  <lose>34</lose>
               </scorable>
            </team>
<!---And so on, you get the idea-->  
         </teams>
      </level>
  </levels>
</NCAAScores>

If I want to retrieve all of the parent level having team "Dallas Stars" what would be the approach?

I tried with below

./levels[/level/teams/team/name = 'Dallas Stars']

, which didn't help.

2
  • How about ./levels[level/teams/team/name = 'Dallas Stars'] Commented May 21, 2018 at 4:01
  • Thanks @JoelM.Lamsen , with that I'm getting "Type error at char 1 in xsl:value-of/@select on line 78 column 81 XPTY0019: Required item type of first operand of '/' is node(); supplied value has item type xs:string" Commented May 21, 2018 at 13:14

2 Answers 2

3

Please try the following template. This uses the ancestor:: axis to look up for level/name from the current node context.

<xsl:template match="level">
    <xsl:for-each select="teams/team[name='Dallas Stars']">
        <name>
            <xsl:value-of select="ancestor::level/name" />
        </name>
    </xsl:for-each>
</xsl:template>

Alternately, you can also use

<xsl:value-of select="../../name" />

to go up the levels from the current node context, but I prefer using ancestor for ease of understanding.

Output

<name>Western Conference</name>
<name>Eastern Conference</name>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Ankit V , that did help
1

The type error you mentioned comes from comparing a node with a string. You have to first extract the text from the node. Try this:

//levels/level[teams/team/name/text()='Dallas Stars']/name

I prepared it as a fiddle here: https://xsltfiddle.liberty-development.net/6qVRKw2/1

1 Comment

Thanks @fafl , that helps, however , while I'm calling that from another template , I'm always getting Required item type of the context item for the child axis is node(); supplied value has item type xs:string Please see the fiddle Line 79 xsltfiddle.liberty-development.net/bFDb2C6

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.