[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Template-matching nodes from input vs nodes in variable based on their position


Subject: Re: [xsl] Template-matching nodes from input vs nodes in variable based on their position
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 18 Jun 2015 09:39:36 -0000

Alternative approach: donbt use position().

<xsl:function name=bmy:indexb as=bxs:integer*b>
  <xsl:param name=bsequenceb as=bnode()*b/>
  <xsl:param name=bnodeb as=bnode()b/>
  <xsl:sequence select=bfor $i in 1 to count($sequence) return if
($sequence[$i] is $node) then $i else ()b/>
</xsl:function>

<xsl:template match=bpage[my:index($FOO, .)] = (1, 3, 5)]b>
  b&
</xsl:template>

Michael Kay
Saxonica

> On 17 Jun 2015, at 19:55, Jorge . chocolate.camera@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> I am puzzled by this. I cannot template-match elements based on their
> position when they are stored in a variable while I can successfully
> do so when they come from the input XML document.
>
> This is the input XML document:
>
>    <?xml version="1.0" encoding="UTF-8"?>
>    <pages>
>        <page>Page number 1</page>
>        <page>Page number 2</page>
>        <page>Page number 3</page>
>        <page>Page number 4</page>
>        <page>Page number 5</page>
>        <page>Page number 6</page>
>        <page>Page number 7</page>
>    </pages>
>
> In the stylesheet below I define a global variable $FOO that contains
> a subtree of elements identical to the input XML (sans root element).
> Then I try to do an identity copy of both the input and the variable,
> but also add an attribute to elements of certain positions.
>
> This is the stylesheet:
>
>    <?xml version="1.0" encoding="UTF-8"?>
>    <xsl:stylesheet version="2.0"
>                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
>        <xsl:variable name="FOO" as="node()+">
>            <page>Page number 1</page>
>            <page>Page number 2</page>
>            <page>Page number 3</page>
>            <page>Page number 4</page>
>            <page>Page number 5</page>
>            <page>Page number 6</page>
>            <page>Page number 7</page>
>        </xsl:variable>
>
>        <xsl:template match="pages">
>            <pages>
>                <!-- Copy input subtree -->
>                <from-input>
>                    <xsl:apply-templates select="@*|node()"/>
>                </from-input>
>
>                <!-- Copy variable subtree -->
>                <from-variable>
>                    <xsl:apply-templates select="$FOO"/>
>                </from-variable>
>            </pages>
>        </xsl:template>
>
>        <!-- Indentity copy -->
>        <xsl:template match="@*|node()">
>            <xsl:copy>
>                <xsl:apply-templates select="@*|node()"/>
>            </xsl:copy>
>        </xsl:template>
>
>        <!-- Add attribute to elements of certain positions -->
>        <xsl:template match="page[position() = (1,3,5)]">
>            <xsl:copy>
>                <xsl:attribute name="foo"/>
>                <xsl:apply-templates select="@*|node()"/>
>            </xsl:copy>
>        </xsl:template>
>
>    </xsl:stylesheet>
>
> As you can see in the output below, only elements from the input
> document are matched, but not elements from the variable.
>
>    <?xml version="1.0" encoding="UTF-8"?>
>    <pages>
>        <from-input>
>            <page foo="">Page number 1</page>
>            <page>Page number 2</page>
>            <page foo="">Page number 3</page>
>            <page>Page number 4</page>
>            <page foo="">Page number 5</page>
>            <page>Page number 6</page>
>            <page>Page number 7</page>
>        </from-input>
>        <from-variable>
>            <page>Page number 1</page>
>            <page>Page number 2</page>
>            <page>Page number 3</page>
>            <page>Page number 4</page>
>            <page>Page number 5</page>
>            <page>Page number 6</page>
>            <page>Page number 7</page>
>        </from-variable>
>    </pages>
>
> Why?


Current Thread
Keywords
xml