Когда я использую «apply-templates» и выбираю последовательность переменных, шаблон воздействует на контекст элемента в последовательности или он влияет на контекст элемента в документе?
В приведенном ниже примере, кажется, что либо, либо нет, но я не понимаю, почему.
<root>
<a/>
<b/>
<c><a/></c>
<a/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="root">
<!--this variable give me a sequence of "a" elements-->
<xsl:variable name="someElementA1" select="//a"/>
<xsl:variable name="someElementA2">
<xsl:copy-of select="//a"/>
</xsl:variable>
<xsl:for-each select="$someElementA1/a">
<xsl:element name="test">
<xsl:text>This is scenario 1: </xsl:text>
<xsl:apply-templates select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="$someElementA2/a">
<xsl:element name="test">
<xsl:text>This is scenario 2: </xsl:text>
<xsl:apply-templates select="."/>
</xsl:element>
</xsl:for-each>
<xsl:element name="test">
<xsl:text>This is scenario 3: </xsl:text>
<xsl:apply-templates select="$someElementA1"/>
</xsl:element>
<xsl:element name="test">
<xsl:text>This is scenario 4: </xsl:text>
<xsl:apply-templates select="$someElementA2/a"/>
</xsl:element>
</xsl:template>
<!--these are the templates to apply-->
<xsl:template match="a[parent::c]">
<xsl:text>This element has a parent</xsl:text>
</xsl:template>
<xsl:template match="a[parent::root]">
<xsl:text>This element is in the root element</xsl:text>
</xsl:template>
<xsl:template match="a">
<xsl:text>This element is in the sequence</xsl:text>
</xsl:template>
</xsl:stylesheet>
ВЫВОД:
<test>This is scenario 2: This element is in the sequence</test>
<test>This is scenario 2: This element is in the sequence</test>
<test>This is scenario 2: This element is in the sequence</test>
<test>This is scenario 3: This element is in the root elementThis element has a parentThis element is in the root element</test>
<test>This is scenario 4: This element is in the sequenceThis element is in the sequenceThis element is in the sequence</test>