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.
Why is //*[1] different from (//*)[1]
Question
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?
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow