user898465
user898465

Reputation: 944

Doing a for-each in XSLT for certain nodes

Hi guys I have a problem here, am completely new to XSLT and it's driving me a bit crazy, as I'm not familiar with the syntax, however the logic seems pretty simple.

So I have an xml of format:

<A>
  <B>
    <C>value1</C>
    <D>amount1</D>
  </B>
</A>
<A>
  <B>
    <C>value2</C>
    <D>amount2</D>
  </B>
</A>

In this case I want to do a for each loop that returns the value of every node C of A/B ONLY WHEN Node D is a certain value. So for example, you could say

for-each A/B, return c, where d = "amount1"

Anyone help out with what the XSLT would be for that?

Upvotes: 0

Views: 543

Answers (2)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

alternatively, you can skip xsl:for-each and use

<xsl:copy-of select="root/A/B[D[.='amount1']]/C"/>

I have added a root tag in your input for testing.

Upvotes: 0

Tim C
Tim C

Reputation: 70648

Try this...

<xsl:for-each select="A/B[D='amount1']">
     <xsl:value-of select="C" />
</xsl:for-each>

Note, this assumed you are currently positioned on the parent element of all the A elements.

Upvotes: 2

Related Questions