I created two files, input.xml and formatter.xslt.

input.xml:

<?xml-stylesheet type="text/xsl" href="formatter.xslt"?>
<div><span>1</span><span>2</span></div>

formatter.xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html><head></head><body>
            <xsl:copy-of select="//*[1]"/>
            ,
            <xsl:copy-of select="(//*)[1]"/>
        </body></html>
    </xsl:template>
</xsl:stylesheet>

Then I opened input.xml in Internet Explorer, and pressed F12. I saw the DOM tree was like:

<html><head></head><body>
    <div><span>1</span><span>2</span></div><span>1</span>
    ,
    <div><span>1</span><span>2</span></div>
</body></html>

Why did //*[1] and (//*)[1] copy different elements into the output document?

有帮助吗?

解决方案

Well //*[1] is /descendant-or-self::node()/*[1] while (//*)[1] is (/descendant-or-self::node()/*)[1] so the former selects all element nodes that are the first child of their parent while the latter selects only the first node selected by the expression in parentheses, that is it selects only the first of those element nodes that are a child of a parent node.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top