【问题标题】:How to check if element node contains a specific value in xsl如何检查元素节点是否包含xsl中的特定值
【发布时间】:2013-04-07 12:59:26
【问题描述】:

我有一个 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 元素是否包含“菠萝”,以便我只能为特定的 fruit 显示 description

这是我的 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>

【问题讨论】:

  • 你有没有考虑过为标题元素使用像 [contains(text(),'pineapple')] 这样的谓词?
  • 我试过
  • 在你的表达中,应该是水果而不是水果。而且您的 xml 格式不正确, 元素未正确关闭。如果你解决这些问题,我认为它会起作用。
  • @user998692 不,我已经纠正了 xml 和 中的错误并再次测试了它,但它不起作用。
  • 我使用该工具测试了 XPath 表达式,它工作正常。我认为 XSLT 代码中可能存在问题。您能否在 XSLT 中显示所有相关元素?

标签: xml xslt xpath-2.0


【解决方案1】:

这是一种稍微推动驱动的方法,可以实现您想要的。

当这个 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>

【讨论】:

  • 这种解决方案很有效,但它也显示西瓜的标题和描述。产生的结果是:一种热带植物,含有可食用的多种水果,由合并的浆果组成我喜欢西瓜,外皮光滑(绿色、黄色,有时是白色),内果肉多汁甜美
  • 您使用的是什么 XSLT 处理器?我使用了几个(Expat、Saxon、libxslt 等),但没有得到您所看到的结果。
  • 好的对不起!您的解决方案有效,@Dimitre Novatchev 的解决方案导致了该结果。
  • @Sujal,是的,我的答案中缺少一行,现在已经到位。
【解决方案2】:

更简单且几乎完全“推送式”的解决方案

<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="/*">
    <html>
      <head>
        <title>Fruits</title>
      </head>
      <body><xsl:apply-templates/></body>
    </html>
 </xsl:template>

 <xsl:template match="fruit[contains(title, 'pineapple')]">
  <xsl:value-of select="description"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

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

产生了想要的正确结果:

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

【讨论】:

  • 这种解决方案很有效,但它也显示西瓜的标题和描述。产生的结果是:一种热带植物,具有可食用的多种水果,由合并的浆果组成我喜欢西瓜,外皮光滑(绿色、黄色,有时是白色)和多汁、甜美的内部果肉
  • @Sujal,抱歉,出现了一个小的复制+粘贴问题。现已更正。
猜你喜欢
  • 1970-01-01
  • 2013-07-31
  • 2012-02-20
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 2020-06-18
  • 2016-07-02
相关资源
最近更新 更多