tags:

views:

40

answers:

2

I have an XML element that looks something like:

<account year="2010">
    <paymentDue>
        <amount>0</amount>
    </paymentDue>
    <paymentDue>
        <amount>752.00</amount>
    </paymentDue>
</account>

I'm displaying the accounts grouped by year and I'm using the <xsl:choose> method to say if amount > 0, display a make payment link. However, I can only make this happen per payment. I'd like to display the payment link for the year if ANY of the payment due amounts are greater than 0.

Any input on how to accomplish this would be appreciated.

+3  A: 

Like this?

<xsl:template match="account">
  <xsl:if test="paymentDue[number(amount) &gt; 0]">
    <!-- display payment link -->
  </xsl:if>
</xsl:template>

The XPath expression paymentDue[number(amount) &gt; 0] selects any <paymentDue> node whose <amount> is greater than 0. The <xsl:if> test succeeds when this results in a non-empty node-set (i.e. when there is at least one node that fulfills the condition).

Tomalak
This does exactly what I need. Thank you. I didn't post my XSLT code because there wasn't anything worth posting. While I've used XSLT for several years, this is the first project where I've used it heavily and am learning a lot about XPath, etc. along the way. I appreciate your help!
Robert
+1  A: 

Try this:

account[paymentDue/amount[text() > 0]]

That is, "Get all account nodes that have at least one payment node that has at least one amount node where the text is greater than 0".

Won't catch errors like having non-numeric values in , but it's a start.

Ron's Brain
You'll want to use `>`, not `>`. Or, better yet, `!=`, as long as that actually is appropriate.
Robert Rossney
You're absolutely correct. Thanks for the correction on this.
Ron's Brain
@Robert-Rossney: The statement in your comment above is untrue. Not only `>` *can* be used, but it is more readable and is to be preferred! Please, consider editing or deleting your comment!
Dimitre Novatchev