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>
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:
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.
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.
A template matching any child element of any student
element. This just wraps the content into a td
element.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With