Home > Mobile >  XSLT link between two elements that share the same value and print the linked data
XSLT link between two elements that share the same value and print the linked data

Time:04-11

I've started learning xml/xslt and xsl-fo and while doing some basic exercises I've got myself into a small issue.

I am trying to link the books to their category price using <category> and <priceCategory> . Not sure what to search for, while browsing stackoverflow I found that I can use a key, like this.

<xsl:key name="link" match="priceCategory" use="priceCategory"/>

and print the value using

<xsl:for-each select="bookstore/books/mybook">
<xsl:value-of select="name"/>
<xsl:value-of select="key('link', money/prices/price)/priceCategory"/>
</xsl:for-each>

But I had no success so far, anyone can help me with some documentation or an example/explanation, I think I am not searching for the right thing, I can't find the right word for this type of problem.

This is an example XML:

<bookstore>
      <books>
            <mybook>
                  <name>bookname1</name>
                  <pages>350</pages>
                  <category>A</category>
            </book>
            <mybook>
                  <name>bookname2</name>
                  <pages>150</pages>
                  <category>B</category>
            </book>
            <mybook>
                  <name>bookname3</name>
                  <pages>450</pages>
                  <category>B</category>
            </book>
            <mybook>
                  <name>bookname4</name>
                  <pages>550</pages>
                  <category>C</category>
            </book>
      <books>
      <money>
            <prices>
                  <price>50</price>
                  <priceCategory> A </priceCategory>
            </prices>
            <prices>
                  <price>100</price>
                   <priceCategory> B </priceCategory>                  
            </prices>
            <prices>
                  <price>150</price>
                  <priceCategory> C </priceCategory>                 
            </prices>
      </money>
</bookstore>


Thank you :)

CodePudding user response:

I suppose you want to show the price of the category? If yes, you could use this:

<xsl:key name="link" match="prices" use="priceCategory"/>

This key wil match on prices using priceCategory when calling the key as "filter" .

<xsl:for-each select="bookstore/books/mybook">
<xsl:value-of select="name"/>
<xsl:value-of select="key('link', category)/price"/>
</xsl:for-each>
  • Related