[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Template-matching nodes from input vs nodes in va

Subject: Re: Template-matching nodes from input vs nodes in variable based on their position
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 17 Jun 2015 19:02:34 -0000
Re:  Template-matching nodes from input vs nodes in  va
Jorge . chocolate.camera@xxxxxxxxx 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?

It is a sequence of elements, try to remove the 'as="node()+"' from the variable and then use
<xsl:apply-templates select="$FOO/*"/>


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.