如何应用XSLT模板规则

时间:2018-02-06 06:39:16

标签: xml xslt xslt-1.0 xslt-2.0

假设我有以下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>

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

  

Everyday ItalianGiada De Laurentiis200530.00Harry PotterJ K.   Rowling200529.99

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

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

任何解释我明显遗失的基本概念的文章指针都会受到赞赏......

1 个答案:

答案 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,但它们可以输出您不明确匹配的元素的任何文本节点。