为什么 IF 条件会在 XSLT 中给出意想不到的结果?
Why does an IF condition gives an unexpected result in XSLT?
我在 XSLT 中制作了 IF
条件的演示。
我想在 id
匹配到 node id
时打印任何 text
。我从这里阅读文档:
https://msdn.microsoft.com/en-us/library/ms256209(v=vs.110).aspx
我使用了 xsl:if
语法。但它没有打印 <p>
标签值。
这是 link 到 XSLTTransform for my problem.
这是我的 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with
XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen of the
world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology society
in England, the young survivors lay the foundation for a new
society.</description>
</book>
</catalog>
我想在图书 ID 'bk101' 时显示 jjj
。
这是我的 XSLT 代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="book">
<xsl:if test="@id =bk101">
<p>jjj</p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
但它没有按预期工作。
select 所有 book
节点的正确 XPath 表达式 book
元素的 id
属性匹配 'bk101' 的值是
book[@id='bk101']
所以一个完整的 XSLT 模板应该是这样的:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="book[@id='bk101']">
<p>jjj</p>
</xsl:template>
</xsl:stylesheet>
@zx485 是正确的,在大多数情况下谓词可能会产生更接近您想要的行为。
就是说,您的 xsl:if
表达式会起作用 - 只需更改一下。您当前的代码包括此测试:
<xsl:if test="@id =bk101">
阻止此功能正常工作的关键问题是引号——或者更确切地说,您缺少引号。
上面的 test
未加引号 bk101
—— 因此 XPath 引擎将其识别为元素名称,因此您最终将属性 id
的值与 id
的值进行比较一个不存在的元素 bk101
。您需要将 bk101
放在引号中以强制 XPath 引擎将其评估为字符串。 (此处使用单引号,以避免与定义 test
表达式的双引号发生语法冲突。)固定行如下所示:
<xsl:if test="@id = 'bk101'">
运行 你的相同代码,修改为添加单引号,在你的示例输入文件的快速和肮脏的转换中为我产生这个输出:
<?xml version="1.0" encoding="UTF-8"?>
<p>jjj</p>
我在 XSLT 中制作了 IF
条件的演示。
我想在 id
匹配到 node id
时打印任何 text
。我从这里阅读文档:
https://msdn.microsoft.com/en-us/library/ms256209(v=vs.110).aspx
我使用了 xsl:if
语法。但它没有打印 <p>
标签值。
这是 link 到 XSLTTransform for my problem.
这是我的 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with
XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen of the
world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology society
in England, the young survivors lay the foundation for a new
society.</description>
</book>
</catalog>
我想在图书 ID 'bk101' 时显示 jjj
。
这是我的 XSLT 代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="book">
<xsl:if test="@id =bk101">
<p>jjj</p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
但它没有按预期工作。
select 所有 book
节点的正确 XPath 表达式 book
元素的 id
属性匹配 'bk101' 的值是
book[@id='bk101']
所以一个完整的 XSLT 模板应该是这样的:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="book[@id='bk101']">
<p>jjj</p>
</xsl:template>
</xsl:stylesheet>
@zx485 是正确的,在大多数情况下谓词可能会产生更接近您想要的行为。
就是说,您的 xsl:if
表达式会起作用 - 只需更改一下。您当前的代码包括此测试:
<xsl:if test="@id =bk101">
阻止此功能正常工作的关键问题是引号——或者更确切地说,您缺少引号。
上面的 test
未加引号 bk101
—— 因此 XPath 引擎将其识别为元素名称,因此您最终将属性 id
的值与 id
的值进行比较一个不存在的元素 bk101
。您需要将 bk101
放在引号中以强制 XPath 引擎将其评估为字符串。 (此处使用单引号,以避免与定义 test
表达式的双引号发生语法冲突。)固定行如下所示:
<xsl:if test="@id = 'bk101'">
运行 你的相同代码,修改为添加单引号,在你的示例输入文件的快速和肮脏的转换中为我产生这个输出:
<?xml version="1.0" encoding="UTF-8"?>
<p>jjj</p>