问题描述:

My input.xml is as follows:

 <root>

<Property>

<info>

<Name>A</Name>

<Value>1000</Value>

</info>

<info>

<Name>B</Name>

<Value>2000</Value>

</info>

<info>

<Name>C</Name>

<Value>3000</Value>

</info>

</Property>

</root>

So here when I say -

<xsl:apply-templates select="//Property/info"> How the tree will be? Can I think it as shown below?

 <Property>

<info>

<Name>A</Name>

<Value>1000</Value>

</info>

<info>

<Name>B</Name>

<Value>2000</Value>

</info>

<info>

<Name>C</Name>

<Value>3000</Value>

</info>

</Property>

and when matching template can I take the Property element as root element in the tree? (I am thinking yes as I executed it and thinking that seperate tree will be created somewhere in memory - Is that true? (explanation please))

<xsl:template match="Property/info"/>

Here my actual Q is- are the templates be applied to the tree in buffer (taking it as main tree) or to the original source tree?

a) If things are calculated/applied on buffered tree, we should not be able to retrieve the root element right when I say

<xsl:copy-of select="../../*"/>

because buffered tree doesn't have root element and processor doesn't know about root element. [But how things are working actually?]

b) If templates are applied to the original source tree then

<xsl:template match="Property/info"/>

should not work right? (as we should give in this way:

<xsl:template match="root/Property/info"/>

or

<xsl:template match="//Property/info"/>

but without mentioning as above its working. How is it possible?)

网友答案:

There are two separate questions here:

1) How are template match patterns matched against parts of the source document tree? and

2) What is the context node when a particular template is applied?

Template match patterns are not the same as XPath expressions, even though they look similar. In particular they do not assume any current context node. "Property/info" matches any info element anywhere in the tree whose parent is a Property element. Even though this match pattern looks the same as the XPath expression in select="Property/info", they are quite different; the latter will select only info elements that are children of Property elements that are children of the context node. The match pattern does not have the latter restriction, nor could it, because the concept of a context node does not enter into the specification for matching match patterns. (I guess it would be more accurate to say that for match patterns, the initial context node is unspecified; it could be any node that has [explicitly or implicitly] been selected by <xsl:apply-templates>.)

Once a particular template is selected (via matching) to be applied to a particular node, that node becomes the context node as the contents of the selected template are evaluated. (The XSLT processor does not (conceptually) copy part of the tree into a buffer; the context node is a node in the original source document.) So yes, you can select "../../*" because you're starting from a context node at /root/Property/info.

相关阅读:
Top