0

I'm studying for an exam, and I'd like to make sure I'munderstanding this correctly. Would the following print out ABC or just BC because the templates weren't applied to next/previous/item?

input.xml :

<?xml version="1.0" encoding="UTF-8"?>
<next>
    <previous>
        <item>A</item>
    </previous>
    <item>B</item>
    <item>C</item>
</next>

input.xsl :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <HowDoes>
            <xsl:apply-templates select="next/item" />
            <xsl:apply-templates select="previous/item" />
        </HowDoes>
    </xsl:template>
    <xsl:template match="item">
        <ThisWork>
            <xsl:copy-of select="." />
        </ThisWork>
    </xsl:template>
</xsl:stylesheet>
4

1 回答 1

0

您的样式表将被忽略A,因为从文档根(您的第一个模板匹配的位置)的角度来看,有两个next/item节点但没有 previous/item节点。只有一个next/previous/item节点。

如果您将模板更改为

<xsl:template match="/">
    <HowDoes>
      <xsl:apply-templates select=".//item" />
    </HowDoes>
</xsl:template>

然后item将找到所有节点,因为它正在寻找当前节点的所有item 后代item,而不是子节点的所有子节点previous

于 2013-04-11T12:21:11.880 回答