user1442404
user1442404

Reputation: 99

XSLT help - Retrieving an ID of an element

I am using XSLT to select and loop through objects with a certain parentID, this is working fine. What I then need to do is check the ID of each selected element of the XML against a value.

Here is my code:

XSL:

<xsl:for-each select="categories/category[@parentId='1333']">
    <xsl:choose>
       <xsl:when test="id='1349'">Yes</xsl:when>
       <xsl:otherwise>No</xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

Here is an example XML block:

 <categories>
    <category !!!!THIS IS THE ID I WANT!!!!! id="1348" parentId="1333">
      <name>Name</name>  
      <description>Desc</description>  
      <path>/test/test/testing</path> 
    <category id="1349" parentId="1333">
      <name>Name</name>  
      <description>Desc</description>  
      <path>/test/test/testing</path> 
    </category>  
    <category id="1352" parentId="1333">
      <name>Name</name>  
      <description>Desc</description>  
      <path>/test/test/testing</path> 
    </category>  
    <category id="1370" parentId="1333">
      <name>Name</name>  
      <description>Desc</description>  
      <path>/test/test/testing</path> 
    </category>  
  </categories> 

Upvotes: 0

Views: 60

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

You don't need an xsl:for-each and xsl:choose to locate and process the wanted element(s).

Just have a template matching the wanted elements:

<xsl:template match="categories/category[@id='1349' and @parentId='1333']">
  <!-- Your processing here -->
</xsl:template>

Upvotes: 1

freefaller
freefaller

Reputation: 19953

I think it's simply that you're not checking for the attribute correctly.

Change...

<xsl:when test="id='1349'"> 

To (note the extra @ which means it's an attribute name rather than an element name)...

<xsl:when test="@id='1349'">

Upvotes: 1

Related Questions