Home > Net >  Count equal lookup values from different nodes. XSLT
Count equal lookup values from different nodes. XSLT

Time:12-12

Count equal lookup values from different nodes. XSLT

Count <book> nodes having the same lookup value from a DB table.For every <code> I make a search in a DB table to find the corresponding value to that code. This value from the lookup I will save into a variable Different <code> numbers can have the same lookup value(For example: /code=11 and code=12 have the same lookup value in DB table so I have to output 2 .For code=13 I have to output 1.

In the output I have to show the counter for distinct lookup values.

 <xsl:for-each select="bookstore/book/code">
  <xsl:variable name="$code" select ="lookup("code","book_code")"/>
    <xsl:value-of select="count(preceding-sibling::$code)"/>
 </xsl:for-each>

INPUT :

`

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>
  <book>
    <code>11</code>
  </book>
  <book>
    <code>12</code>
  </book>
</bookstore>
<bookstore>
  <book>
    <code>13</code>
  </book>
</bookstore>

OUTPUT: 2 1

CodePudding user response:

In XSLT 2 or 3 you would want

<xsl:for-each-group select="bookstore/book" group-by="key('lookup', code, $lookup-doc)">
  <xsl:value-of select="count(current-group())"/>
</xsl:for-each-group>

I think, using e.g. a key for the second document based on `<xsl:key name="lookup" match="some-element" use="book_code"/>.

You haven't really spelled out details of the second document structure or your pseudo code with lookup, so the above can only be adapted to the right key and key call if you show the document structure of the secondary document.

  • Related