Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys I have an xslt file I want to extract data from.

I am new to xslt but at the moment I can perform basic operations like looping through the nodes and creating element and attributes.

My problem is that I want to select a particular value from a particular node(from a list of nodes), here is an example.

<PersonsList><br />
<person><br />
<id>1233</id><br />
<name>john</name><br />
</person><br />
<person><br />
<id>444</id><br />
<name>peter</name><br />
</person><br />
<person><br />
<id>677</id><br />
<name>Amanda</name><br />
</person><br />
</PersonsList>


I want to get for example the name value say amanda if it matches the id. like in C# one would say: if (id==677) select name, Now I want the xslt equivalent.
Posted

The following XSLT creates an HTML table with the respective selected item marked.
The item in question can be passed as id parameter from your xslt processor - default is 677.

XML
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:param name="id">677</xsl:param>

  <xsl:output method="html" indent="yes"/>

    <xsl:template match="/PersonsList">
      <table border="1">
        <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Selected</th>
        </tr>
        <xsl:for-each select="person">
          <xsl:sort select="id" order="ascending" data-type="number"/>
          <tr>
            <td>
              <xsl:value-of select="id"/>
            </td>
            <td>
              <xsl:value-of select="name"/>
            </td>
            <td>
              <xsl:choose>
                <xsl:when test="id=$id">yes</xsl:when>
                <xsl:otherwise>no</xsl:otherwise>
              </xsl:choose>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:template>
</xsl:stylesheet>


Output:
XML
<table border="1">
  <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Selected</th>
  </tr>
  <tr>
    <td>444</td>
    <td>peter</td>
    <td>no</td>
  </tr>
  <tr>
    <td>677</td>
    <td>Amanda</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>1233</td>
    <td>john</td>
    <td>no</td>
  </tr>
</table>


Id Name Selected
444 peter no
677 Amanda yes
1233 john no



Please note that XSLT requires functional programming style, not procedural programming style. Main reason is, that you can only initialize variables, but not assign new values to variables.

Cheers
Andi
 
Share this answer
 
Comments
Sandeep Mewara 31-May-12 9:33am    
5!
Andreas Gieriet 31-May-12 10:14am    
Thanks for your 5!
XPath:
XML
person[id=677]/name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900