【问题标题】:How XSLT Templates Rules are applied如何应用 XSLT 模板规则
【发布时间】:2018-02-06 06:39:16
【问题描述】:

假设我有以下 XML:

XML

<bookstore>
<book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
</book>
<book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
</book>
<book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
</book>
<book category="web" cover="paperback">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
</book>

并遵循 XSLT:

XSLT

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:fn="http://www.w3.org/2005/xpath-functions"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="fo xs fn msxsl">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <xsl:element name="rootnode">
      <xsl:choose>
        <xsl:when test="//bookstore/book[year!=2005]">
          <xsl:message select="'book exists'" />
          <xsl:apply-templates></xsl:apply-templates>
        </xsl:when>
      </xsl:choose>
    </xsl:element>
  </xsl:template>

  <xsl:template match="book[year!=2005]">
    <xsl:element name="not2005">
      <xsl:copy-of select="." />
    </xsl:element>
    <!--<xsl:apply-templates select="@* | node()" />-->
  </xsl:template>
</xsl:stylesheet>

转换后的 xml 如下所示:

结果

<?xml version="1.0" encoding="UTF-8"?>
<rootnode>Everyday ItalianGiada De Laurentiis200530.00Harry PotterJ K. Rowling200529.99<not2005>
        <book category="web">
            <title lang="en">XQuery Kick Start</title>
            <author>James McGovern</author>
            <author>Per Bothner</author>
            <author>Kurt Cagle</author>
            <author>James Linn</author>
            <author>Vaidyanathan Nagarajan</author>
            <year>2003</year>
            <price>49.99</price>
        </book>
    </not2005>
    <not2005>
        <book category="web" cover="paperback">
            <title lang="en">Learning XML</title>
            <author>Erik T. Ray</author>
            <year>2003</year>
            <price>39.95</price>
        </book>
    </not2005>
</rootnode>

而不是我所期待的:

预期结果

<?xml version="1.0" encoding="UTF-8"?>
<rootnode>
    <not2005>
        <book category="web">
            <title lang="en">XQuery Kick Start</title>
            <author>James McGovern</author>
            <author>Per Bothner</author>
            <author>Kurt Cagle</author>
            <author>James Linn</author>
            <author>Vaidyanathan Nagarajan</author>
            <year>2003</year>
            <price>49.99</price>
        </book>
    </not2005>
    <not2005>
        <book category="web" cover="paperback">
            <title lang="en">Learning XML</title>
            <author>Erik T. Ray</author>
            <year>2003</year>
            <price>39.95</price>
        </book>
    </not2005>
</rootnode>

为什么跟随被转换/拾取?

日常意大利语Giada De Laurentiis200530.00哈利波特J K. 罗琳200529.99

还很好奇为什么我的 xsl:message 也没有弹出?

<xsl:message select="'book exists'" />

任何解释我显然缺少的基本概念的文章指针将不胜感激......

【问题讨论】:

  • 恭喜:与这里的一半人不同,您不只是想剪切和粘贴示例代码,您还想了解概念。我写我的书时考虑到像你这样的人:来自 Wiley 的 XSLT 2.0/XPath 2.0 程序员参考。
  • @MichaelKay 感谢迈克尔的这些客气话

标签: xml xslt xslt-1.0 xslt-2.0


【解决方案1】:

改变

  <xsl:choose>
    <xsl:when test="//bookstore/book[year!=2005]">
      <xsl:message select="'book exists'" />
      <xsl:apply-templates></xsl:apply-templates>
    </xsl:when>
  </xsl:choose>

<xsl:apply-templates select="bookstore/book[year != 2005]"/>

您当前的代码仅在您依赖内置模板 https://www.w3.org/TR/xslt20/#built-in-rule 时有效,但它们具有输出您未明确匹配的元素的任何文本节点的效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多