user3669116
user3669116

Reputation: 23

XSL inside for-each logic on count less than 2

Sample xml from the big xml:

<result type="FLP" id="FL">
    <PPR>
        <PP>
            <FID id="L1001">18084</FID>
            <FNAME id="L1002">some text </FNAME>
            <PRM_KEY id="L1349">Yes</PRM_KEY>
            <CURRENCY_CODE id="L1821">CAD</CURRENCY_CODE>
        </PP>
        <PP>
            <FID id="L1001">18084</FID>
            <FNAME id="L1002">some text </FNAME>
            <PRM_KEY id="L1349">No</PRM_KEY>
            <CURRENCY_CODE id="L1821">CAD</CURRENCY_CODE>
        </PP>
        <PP>
            <FID id="L1001">18084</FID>
            <FNAME id="L1002">some text </FNAME>
            <PRM_KEY id="L1349">Yes</PRM_KEY>
            <CURRENCY_CODE id="L1821">USD</CURRENCY_CODE>
        </PP>
        <PP>
            <FID id="L1001">18084</FID>
            <FNAME id="L1002">some text </FNAME>
            <PRM_KEY id="L1349">No</PRM_KEY>
            <CURRENCY_CODE id="L1821">CAD</CURRENCY_CODE>
        </PP>
        <PP>
            <FID id="L1001">18084</FID>
            <FNAME id="L1002">some text </FNAME>
            <PRM_KEY id="L1349">Yes</PRM_KEY>
            <CURRENCY_CODE id="L1821">EUR</CURRENCY_CODE>
        </PP>
    </result>

I want to display only FNAME value under my table td when PRM_KEY is Yes but FNAME values are displaying three times since there is no condition for CURRENCY_CODE. I want to display FNAME only once when PRM_KEY is Yes. I tried below xsl condition but that did not help since count value was always one

    <xsl:for-each select="/result[@type='FLP']/PPR/PP[PRM_KEY = 'Yes'] and count(CURRENCY_CODE) < 2">
    <tr>
    <td><xsl:value-of select="FNAME"/></td>
<h4>count val is: <xsl:value-of select="count(CURRENCY_CODE)"/></h4>
    </tr>
    </xsl:for-each>

Upvotes: 0

Views: 77

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117140

I have requirement for PRM_KEY is Yes then it's FNAME should display since all FNAME values are same so it should display only once.

If I take your requirement literally, then it should be sufficient to display the first FNAME value when at least one PP satisfies the condition of PRM_KEY='Yes'. This can be done very simply, without using xsl:for-each at all:

<xsl:template match="/result">
    <xsl:if test="PPR/PP[PRM_KEY='Yes']">
        <output>
            <xsl:value-of select="PPR/PP[1]/FNAME" />
        </output>
    </xsl:if>
</xsl:template>

OTOH, it's possible you want to display each distinct FNAME value - in which case your question is about grouping and you can find the answer here.

Upvotes: 1

Valdi_Bo
Valdi_Bo

Reputation: 31011

The "records" (items) you are interested in are PP tags with PRM_KEY = 'Yes'.

Then you want to divide them into groups with equal FNAME.

For each of the above groups you want to display:

  • the FNAME (equal for all items in the group),
  • how many items (in this group) have defined CURRENCY_CODE.

Below you have an XSLT script performing this task:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*"/>

  <xsl:key name="ppItems" match="PP[PRM_KEY = 'Yes']" use="FNAME"/>

  <xsl:template match="result[@type='FLP']/PPR">
    <table>
      <xsl:for-each select="PP[generate-id() = generate-id(key('ppItems', FNAME)[1])]">
        <xsl:variable name="kk" select="key('ppItems', FNAME)"/>
        <tr>
          <td><xsl:value-of select="FNAME"/></td>
          <td>Count: <xsl:value-of select="count($kk/CURRENCY_CODE)"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

</xsl:transform>

In order to keep proper structure of HTML, I changed your h4 tag into another td.

Upvotes: 1

Related Questions