4

suppose i have this xml:

<college>
    <student>
        <name>amit</name>
        <file>/abc/kk/final.c</file>
        <rollno>22</rollno>
    </student>
    <student>
        <name>sumit</name>
        <file>/abc/kk/up.h</file>
        <rollno>23</rollno>
    </student>
    <student>
        <name>nikhil</name>
        <file>/xyz/up.cpp</file>
        <rollno>24</rollno>
    </student>
    <student>
        <name>bharat</name>
        <file>/abc/kk/down.h</file>
        <rollno>25</rollno>
    </student>
    <student>
        <name>ajay</name>
        <file>/simple/st.h</file>
        <rollno>27</rollno>
    </student>
</college>

i am using for-each in ".xsl" to display all the entries of nodes, but i only want to display the entries of those node only in which file name starts with "/abc/kk" as i am new to xslt..

please provide me solution.

i am using :

<xsl:for-each select="college/student">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="file"/></td>
<td><xsl:value-of select="rollno"/></td>
</tr>
2
  • 2
    Please provide nicely formatted XML so we can better understand your question Commented Apr 7, 2011 at 9:07
  • Good question, +1. See my answer for a complete, short and easy solution that uses the fundamental features of XSLT such as templates and push style processing. A detailed explanation is provided. Commented Apr 7, 2011 at 13:30

3 Answers 3

7

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>


 <xsl:template match="student[starts-with(file,'/abc/kk')]">
  <tr><xsl:apply-templates/></tr>
 </xsl:template>

 <xsl:template match="student/*">
     <td><xsl:apply-templates/></td>
 </xsl:template>

 <xsl:template match="student"/>    
</xsl:stylesheet>

when applied to the provided XML document:

<college>
    <student>
        <name>amit</name>
        <file>/abc/kk/final.c</file>
        <rollno>22</rollno>
    </student>
    <student>
        <name>sumit</name>
        <file>/abc/kk/up.h</file>
        <rollno>23</rollno>
    </student>
    <student>
        <name>nikhil</name>
        <file>/xyz/up.cpp</file>
        <rollno>24</rollno>
    </student>
    <student>
        <name>bharat</name>
        <file>/abc/kk/down.h</file>
        <rollno>25</rollno>
    </student>
    <student>
        <name>ajay</name>
        <file>/simple/st.h</file>
        <rollno>27</rollno>
    </student>
</college>

produces the wanted, correct result:

<tr>
   <td>amit</td>
   <td>/abc/kk/final.c</td>
   <td>22</td>
</tr>
<tr>
   <td>sumit</td>
   <td>/abc/kk/up.h</td>
   <td>23</td>
</tr>
<tr>
   <td>bharat</td>
   <td>/abc/kk/down.h</td>
   <td>25</td>
</tr>

Explanation:

  1. A template matching any student having a file child whose string value starts with '/abc/kk'. This just puts the generated contents in a wrapper tr element.

  2. A template matching any student that has no body and effectively deletes it (doesn't copy this element to the output). This template has a lower priority than the first one, because the first one is more specific. Therefore, only student elements that are not matched by the first template are processed with the second template.

  3. A template matching any child element of any student element. This just wraps the content into a td element.

Sign up to request clarification or add additional context in comments.

Comments

3

Like this:

<xsl:for-each select="college/student[starts-with(file, '/abc/kk')]">
<!-- ... -->

The brackets [ ] delimit a "filter", and inside that filter you can have functions like starts-with()

1 Comment

@kuldeep: please check this answer as correct. It will help you to get more people willing to answer your further question on SO.
0

Also you can use [..] in match:

<xsl:template match="college/student[starts-with(file, '/abc/kk')]">
    <tr>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="file"/></td>
        <td><xsl:value-of select="rollno"/></td>
    </tr>
</xsl:template>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.