how to filter nodes in xml using xslt..?

后端 未结 3 991
情歌与酒
情歌与酒 2021-01-14 00:13

suppose i have this xml:


    
        amit
        /abc/kk/final.c
        &         


        
相关标签:
3条回答
  • 2021-01-14 01:03

    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.

    0 讨论(0)
  • 2021-01-14 01:10

    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>
    
    0 讨论(0)
  • 2021-01-14 01:11

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

    0 讨论(0)
提交回复
热议问题