wbeard52
wbeard52

Reputation: 103

Count nodes matching string pattern in XML

How do I count the number of times the words LIST, BUY or SELL are in the chart_name node of this XML document? I am trying to figure out how many times each are listed in that node from the parent node test_name.

<?xml version="1.0" encoding="utf-8"?>
<digital1>
  <test_name ID="Test">
    <record>
      <chart_name>LIST OR BUY Test 1</chart_name>
    </record>
    <record>
      <chart_name>LIST Test 2</chart_name>
    </record>
  </test_name>
  <test_name ID="Ryan">
    <record>
      <chart_name>BUY Ryan 1</chart_name>
    </record>
    <record>
      <chart_name>LIST Ryan 2</chart_name>
    </record>
    <record>
      <chart_name>SELL OR LIST Ryan 3</chart_name>
    </record>
    <record>
      <chart_name>LIST OR BUY Ryan 4</chart_name>
    </record>
    <record>
      <chart_name>BUY Ryan 5</chart_name>
    </record>
    <record>
      <chart_name>LIST Ryan 6</chart_name>
    </record>
  </test_name>
</digital_tpp>

The XSLT file I am using looks like this:

<xsl:template match="/">
  <html>
  <body>
  <h2>My Test File</h2>
      <xsl:for-each select="digital1/test_name/record]">
        <tr>
          <td><xsl:value-of select="../@ID"/></td>
          <td><xsl:value-of select="count(chart_name[. Like '*LIST*'])"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

It is this line <td><xsl:value-of select="count(chart_name[. Like '*LIST*'])"/></td> I am needing help with. How do I do a pattern match to match those key words above?

The output will be a table showing test_name ID, count of LIST, count of BUY and count of SELL.

Upvotes: 0

Views: 254

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

If I understand correctly, you want to do:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/digital1">
    <html>
        <body>
            <h2>My Test File</h2>
            <table border="1">
                <tr>
                    <th>ID</th>
                    <th>LIST</th>
                    <th>BUY</th>
                    <th>SELL</th>
                </tr>
                <xsl:for-each select="test_name">
                    <tr>
                        <td>
                            <xsl:value-of select="@ID"/>
                        </td>
                        <td>
                            <xsl:value-of select="count(record[contains(chart_name, 'LIST')])"/>
                        </td>
                        <td>
                            <xsl:value-of select="count(record[contains(chart_name, 'BUY')])"/>
                        </td>
                        <td>
                            <xsl:value-of select="count(record[contains(chart_name, 'SELL')])"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body> 
    </html>
</xsl:template>

</xsl:stylesheet>

Applied to your input example (after correcting </digital_tpp> to </digital1>), the result will be:

enter image description here

Upvotes: 2

Related Questions