首页 > 解决方案 > XSLT 匹配输出匹配模板外部的值

问题描述

为什么 xsl:template 匹配 xslt 中未引用的节点的输出值?为了了解发生了什么,我从 xslt 中删除了所有代码,并查看包含所有节点值的输出?为什么是这样?我希望输出 xsl 应该只包含那些在 xslt 中显式映射的节点的值。这是一个例子。这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
    <cd>
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
    <cd>
        <title>Eros</title>
        <artist>Eros Ramazzotti</artist>
        <country>EU</country>
        <company>BMG</company>
        <price>9.90</price>
        <year>1997</year>
    </cd>
    <cd>
        <title>One night only</title>
        <artist>Bee Gees</artist>
        <country>UK</country>
        <company>Polydor</company>
        <price>10.90</price>
        <year>1998</year>
    </cd>
    <cd>
        <title>Sylvias Mother</title>
        <artist>Dr.Hook</artist>
        <country>UK</country>
        <company>CBS</company>
        <price>8.10</price>
        <year>1973</year>
    </cd>
    <cd>
        <title>Maggie May</title>
        <artist>Rod Stewart</artist>
        <country>UK</country>
        <company>Pickwick</company>
        <price>8.50</price>
        <year>1990</year>
    </cd>
</catalog>

这是我用来查看为什么节点值在输出中的 xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>

如您所见,我没有模板匹配来转换 XML 中的任何数据。以下是我得到的输出:

Empire Burlesque Bob Dylan USA Columbia 10.90 1985 隐藏你的心 Bonnie Tyler 英国 CBS Records 9.90 1988 最热门歌曲 Dolly Parton 美国 RCA 9.90 1982 Still got the blues Gary Moore UK Virgin Records 10.20 1990 Eros Eros Ramazzotti EU BMG 9.90 1997 只有一晚 Bee Gees UK Polydor 10.90 1998 Sylvias Mother Dr.Hook UK CBS 8.10 1973 Maggie May Rod Stewart UK Pickwick 8.50 1990

为什么会这样?

标签: xmlxslt

解决方案


您看到的原因是内置模板规则

内置模板处理您没有匹配模板的元素:

<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

其子文本节点通过以下方式复制到输出:

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

推荐阅读