如何使用 xslt 过滤 xml 中的节点? [英] how to filter nodes in xml using xslt..?

查看:45
本文介绍了如何使用 xslt 过滤 xml 中的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个 xml:

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>

我在.xsl"中使用 for-each 来显示节点的所有条目,但我只想显示那些文件名以/abc/kk"开头的节点的条目,因为我是新手到 xslt..

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..

请给我解决方案.

我正在使用:

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

推荐答案

这种转变:

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

应用于提供的 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>

产生想要的、正确的结果:

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

说明:

  1. 匹配任何具有 file 子级的 student 的模板,其字符串值以 '/abc/kk' 开头.这只是将生成的内容放在包装器 tr 元素中.

  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.

匹配任何 student 的模板,它没有正文并有效地删除它(不将此元素复制到输出).此模板的优先级低于第一个,因为第一个更具体.因此,只有与第一个模板不匹配的 student 元素被第二个模板处理.

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.

匹配任何student 元素 的任何子元素的模板.这只是将内容包装到 td 元素中.

这篇关于如何使用 xslt 过滤 xml 中的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆