XSLT select // and .//

What is the difference between // .//

when setting a path? And the second question is, do I want to get information about the parent node in each block, how can I get it?

+3


source to share


1 answer


//

is short for the child axis. If you say //para

it selects all elements para

in the whole document.

When you say .//para

, all elements para

that are descendants of the node context are selected .

For a demonstration, consider this XML:

<l1>
<para>1</para>
<l2>
    <para>2</para>
    <l3>
        <para>3</para>
    </l3>
</l2>
</l1>

      

and this XSLT:



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/l1">
    <output>
        <xsl:apply-templates select="l2"/>
    </output>
</xsl:template>
<xsl:template match="l2">
    <para-all>
        <xsl:copy-of select="//para"/>
    </para-all>
    <para-context>
        <xsl:copy-of select=".//para"/>
    </para-context>
    <parent>
        <xsl:value-of select="name(parent::*)"/>
    </parent>
</xsl:template>
</xsl:stylesheet>

      

And you get:

<?xml version="1.0" encoding="utf-8"?>
<output>
<para-all>
  <para>1</para>
  <para>2</para>
  <para>3</para>
</para-all>
<para-context>
  <para>2</para>
  <para>3</para>
</para-context>
<parent>l1</parent>
</output>

      

para-all

gets everything para

in the document, whereas para-context

only gets those para

that are descendants l2

(context node)

And how to select a parent, as in code (see element parent

in XSLT). In the above example, the context node is equal l2

and using the parent axis, the name of the parent is written. You can use either parent::*

or../

+3


source







All Articles