xml 如何检查元素节点是否包含 xsl 中的特定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15862655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if element node contains a specific value in xsl
提问by Sujal
I have an XML document:
我有一个 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>
How do I check if the titleelement contains 'pineapple' so that i can only display descriptionfor that particular fruit?
如何检查title元素是否包含“菠萝”,以便我只能显示description该特定元素fruit?
Here is the XSLT transformation I have:
这是我拥有的 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>
回答by ABach
Here's a slightly more push-driven approach that accomplishes what you want.
这是一种稍微更推动驱动的方法,可以完成您想要的。
When this XSLT:
当这个 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>
...is applied to the provided XML:
...应用于提供的 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>
...the wanted result is produced:
...产生了想要的结果:
<?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>
回答by Dimitre Novatchev
A simpler and almost fully "push style" solution:
一个更简单且几乎完全“推式”的解决方案:
<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>
When this transformation is applied on the provided XML document:
当此转换应用于提供的 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>
the wanted, correct result is produced:
产生了想要的、正确的结果:
<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>

