Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 3rd, 2007, 07:14 PM
Registered User
 
Join Date: Dec 2006
Location: Berlin, Berlin, .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generic templates and only one node()

Hi folks,
is it possible to program generic templates, which iterate through only one node-set? So I have one node-set, I get it with document(), and I want to rename elements on it. With the code below I get exactly the same number of trees, how /rule/rules//pattern matched. I know that node()* is an array, but I don't have any idea for a better strategy, because variables aren't changeable.
Code:
<xsl:template match="/">        
  <xsl:variable name="tree" as="node()*">
    <xsl:apply-templates select="//pattern"/>
  </xsl:variable>
<!--in $tree are x-trees and in every tree the rename 
of the process before is loosed. If I could change this fact, I 
could output the last tree from this array and it would be ok.-->
  <xsl:for-each select="$tree">
   <xsl:copy-of select="."/>
  </xsl:for-each>
</xsl:template>


<xsl:template match="/rule/rules//pattern" as="node()">
  <xsl:param name="document" select="document(another File)"/>
  <xsl:apply-templates select="$document" mode="document_">
  <xsl:with-param .. few parameter/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="@*|node()" as="node()" mode="document_">
  <xsl:param ..get here with-param/>
  <xsl:choose>
    <xsl:when test"..">
      <xsl:element name="..">
        <xsl:apply-templates select="@*|node()" mode="document_"/>
      </xsl:element>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy>
        <xsl:apply-templates select="@*|node()" mode="document_"/>
      </xsl:copy>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>
Thanks for help.

Clyne


Reply With Quote
  #2 (permalink)  
Old January 3rd, 2007, 07:25 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Without knowing what your input is and what output you want to produce, I can't possibly make any suggestions on how you should write your code.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old January 3rd, 2007, 07:58 PM
Registered User
 
Join Date: Dec 2006
Location: Berlin, Berlin, .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

..thanks for help

Code:
<rule>
  <rules>
    <pattern>
      <xpath attribute1="p1" attribute2="p1_"/>
    </pattern>
    <pattern>
      <xpath attribute1="p2" attribute2="p2_"/>
    </pattern>
  </rules>
</rule>


<test>
  <p1/>
  <p2/>
  <p3/>
</test>

<xsl:for-each select="$tree">
   <xsl:copy-of select="."/>
  </xsl:for-each>

<test>
  <p1_/>
  <p2/>
  <p3/>
</test>
<test>
  <p1/>
  <p2_/>
  <p3/>
</test>


<test>
  <p1_/>
  <p2_/>
  <p3/>
</test>
Clyne

Reply With Quote
  #4 (permalink)  
Old January 4th, 2007, 08:21 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try something like this (you seem to be using XSLT 2.0):

<xsl:variable name="rulesdoc" select="/"/>
<xsl:variable name="testdoc" select="document('test.xml')"/>

<xsl:key name="mapping" match="xpath" use="@attribute1"/>

<xsl:template match="/">
  <xsl:apply-templates select="$testdoc/*"/>
</xsl:template>

<xsl:template match="test">
  <xsl:copy>
    <xsl:for-each select="*">
       <xsl:element name="{key('mapping', name(), $rulesdoc)/@attribute2}"/>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old January 5th, 2007, 12:47 AM
Registered User
 
Join Date: Dec 2006
Location: Berlin, Berlin, .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

With my sincere thanks, it works perfectly.

Clyne

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Templates & Generic Programming shazia1 VS.NET 2002/2003 0 March 29th, 2006 12:13 PM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
Understanding context node in templates Birger XSLT 3 August 31st, 2004 04:21 AM



All times are GMT -4. The time now is 08:17 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.