Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to filter nodes in xml using xslt..?

Tags:

xslt

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>
like image 243
kuldeep Avatar asked Dec 27 '22 22:12

kuldeep


2 Answers

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.

like image 110
Dimitre Novatchev Avatar answered Jan 24 '23 18:01

Dimitre Novatchev


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()

like image 34
Lukas Eder Avatar answered Jan 24 '23 18:01

Lukas Eder