加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 如何检查元素节点是否包含xsl中的特定值

发布时间:2020-12-16 23:20:25 所属栏目:百科 来源:网络整理
导读:我有一个 XML文档: ?xml version="1.0" encoding="ISO-8859-1"?document fruits fruit id="1" titleI like pineapples/title description a tropical plant with edible multiple fruit consisting of coalesced berries/description /fruit fruit id="2" t
我有一个 XML文档:

<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
    <fruits>
        <fruit id="1">
            <title>I like pineapples</title>
            <description> a tropical plant with edible multiple fruit consisting of coalesced berries</description>
        </fruit>
        <fruit id="2">
            <title>I like watermelons</title>
            <description>has a smooth exterior rind (green,yellow and sometimes white) and a juicy,sweet interior flesh</description>
        </fruit>
    </fruits>
</document>

如何检查title元素是否包含’菠萝’,以便我只能显示该特定水果的描述?

这是我的XSLT转换:

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output  method="xml" omit-xml-declaration="yes" doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
             doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd"/>
 <xsl:template match="/">
    <xsl:element name="html">
      <xsl:element name="head">        
        <xsl:element name="title">Fruits</xsl:element>
      </xsl:element>

      <xsl:element name="body">
        <xsl:if test="/document/fruits/fruit/title[contains(text(),'pineapple')]">
          <xsl:value-of select="description"/>
        </xsl:if>
        </xsl:element>
    </xsl:element>
  </xsl:template>
 </xsl:stylesheet>

解决方法

这是一种稍微更推动的方法,可以实现您想要的效果.

当这个XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes"
  doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
  doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd" />
  <xsl:strip-space elements="*" />

  <xsl:template match="/*">
    <html>
      <head>
        <title>Fruits</title>
      </head>
      <body>
        <xsl:apply-templates
          select="fruits/fruit[contains(title,'pineapple')]" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="fruit">
    <xsl:apply-templates select="description" />
  </xsl:template>
</xsl:stylesheet>

…应用于提供的XML:

<?xml version="1.0" encoding="utf-8"?>
<document>
  <fruits>
    <fruit id="1">
      <title>I like pineapples</title>
      <description>a tropical plant with edible multiple fruit
      consisting of coalesced berries</description>
    </fruit>
    <fruit id="2">
      <title>I like watermelons</title>
      <description>has a smooth exterior rind (green,yellow and
      sometimes white) and a juicy,sweet interior
      flesh</description>
    </fruit>
  </fruits>
</document>

…产生了想要的结果:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Fruits</title>
  </head>
  <body>a tropical plant with edible multiple fruit consisting of coalesced berries</body>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读