0

鉴于此 XML:

<?xml version="1.0" encoding="iso-8859-2" ?>
    <products>
        <p>
            <id> 50 </id>
            <name> Murphy </name>
            <price> 33 </price>
        </p>
        <p>
            <id> 40 </id>
            <name> Eddie </name>
            <price> 9999 </price>
        </p>
        <p>
            <id> 20 </id>
            <name> Honey </name>
            <price> 9999 </price>
        </p>
        <p>
            <id> 30 </id>
            <name> Koney </name>
            <price> 11 </price>
        </p>
        <p>
            <id> 10 </id>
            <name> Margarethe </name>
            <price> 11 </price>
        </p>
    </products>

使用此 XSL:

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

<xsl:template match="p[id &gt; 20]">
    <idKP>   <xsl:value-of select="id"/></idKP>
    <skitter><xsl:value-of select="name"/></skitter>
</xsl:template>


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

有这个输出:

<?xml version="1.0"?>
<idKP>50</idKP><skitter>Murphy</skitter>
<idKP>40</idKP><skitter>Eddie</skitter>
20
Honey
9999
<idKP>30</idKP><skitter>Koney</skitter>
10
Margarethe
11

Q:为什么会有不匹配的值?20, Honey, 9999, ...

4

2 回答 2

1

由于内置模板规则- 当没有明确的模板来匹配特定节点时,使用内置规则,元素节点的意思<xsl:apply-templates/>是文本节点,文本节点的意思是<xsl:value-of select="."/>。这两个规则结合起来的效果是输出元素下的所有文本,而不是元素标签本身。

您可以添加第二个无操作模板

<xsl:template match="p" />

完全忽略p不符合您条件的元素。显式模板,甚至是无所事事的模板,都比默认的内置规则更受欢迎。

于 2015-06-10T18:47:25.713 回答
0

为了增加答案,这是解决方案:

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

<xsl:template match="p[id &gt; 20]">
    <idKP>   <xsl:value-of select="id"/></idKP>
    <skitter><xsl:value-of select="name"/></skitter>
</xsl:template>

<xsl:template match="p"/>

</xsl:stylesheet>
于 2015-06-10T19:12:48.920 回答