|
Subject: [xsl] Template-matching nodes from input vs nodes in variable based on their position From: "Jorge . chocolate.camera@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Wed, 17 Jun 2015 18:55:22 -0000 |
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 |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] XPATH 1.0: Selecting an e, Michael Kay mike@xxx | Thread | Re: [xsl] Template-matching nodes f, Martin Honnen martin |
| Re: [xsl] XPATH 1.0: Selecting an e, Liam R. E. Quin liam | Date | Re: [xsl] Template-matching nodes f, Martin Honnen martin |
| Month |