Ibrarr Khan
Ibrarr Khan

Reputation: 55

How to access nested elements in an if statement XSLT

So I need to access a nested element in an if statement.

Below you can see an example of the XML I am using:

<Publication>
   <PubName>Avoid the Consumer Apps - How to Collaborate Securely and Productively in the Finance Sector</PubName>
   <Attributes>
    <Attribute>
     <AttributeName>Type</AttributeName>
     <Value>Webinar</Value>
     <ValueText>Webinar</ValueText>
    </Attribute>
   </Attributes>
  </Publication>

And here is the XSLT code I am using to try and get to the Webinar value:

<xsl:for-each select="TradePub.com/PublicationTable/Publication">
<xsl:if test="Attributes/Attribute/Value='Webinar'">
    <tr>
      <td><xsl:value-of select="PubName"/></td>
      <td><xsl:value-of select="PubCode"/></td>
    </tr>
</xsl:if>
</xsl:for-each>

But this returns nothings, so I am wondering how I can access the Value element?

Upvotes: 1

Views: 252

Answers (1)

zx485
zx485

Reputation: 29052

Use a predicate on the Value element like this:

<xsl:for-each select="TradePub.com/PublicationTable/Publication">
    <xsl:if test="Attributes/Attribute[Value!='Webinar']">
        <tr>
        <td><xsl:value-of select="PubName"/></td>
        <td><xsl:value-of select="PubCode"/></td>
        </tr>
    </xsl:if>
</xsl:for-each>

Another problem why you don't get any output is that the IF-clause is FALSE in your sample. To get the desired output with the given sample, use

<xsl:if test="Attributes/Attribute[Value='Webinar']">

instead. Then, the output would be

<tr>
  <td>Avoid the Consumer Apps - How to Collaborate Securely and Productively in the Finance Sector</td>
  <td/>    <!-- No 'PubCode' element present -->
</tr>

Upvotes: 2

Related Questions