Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 21st, 2012, 07:30 AM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default Apply changes to two different paths

Dear all,

I was wondering if it is possible to use <xsl:apply-templates> to apply changes to a specific (not current) location of the original xml file.

Imagine an xml file composed by many repetitions of the following pattern (i.e. many grandfathers). For brevity all nodes are empty, but this is not the case in the real file.


Code:
<grandfather>
  <father/>
    <child/>
      <grandson/>

  <mother/>
    <child/>
      <grandson/>
</grandfather>
What I would like to do is the following:
Code:
<xsl:template match="/">
  <xsl:for-each select="grandfather/father/child">
    <!-- if specific condition "A" on a grandson's attribute holds -->
      <xsl:result-document ...>
          <xsl:apply-templates select="$document"/>
      </xsl:result-document ...>
  </xsl:for-each>
</xsl:template>


<xsl:template match="grandfather/father/child">
  <!-- if condition "A" on a grandson's attribute holds -->
  Apply CHANGES HERE
  <!-- if a specific condition "B" on current()/parent::*/parent::*/mother/child  holds-->
   <xsl:apply-templates ... mode="mother" />
</xsl:template>

<identity-template>
  to copy everything that has to remain as it is
</identity-template>

<xsl:template ... mode="mother">
 changes to mother's child
</xsl:template>
The problem is that the changes to the mother's child are introduced in the "grandfather/father/child" path, because the apply-templates tag is inside the xsl:template command matching "grandfather/father/child". And I want to generate a file for each occurrence of a "grandfather/father/child" for which the specific condition A holds.

Is there any way to tell where the result of the application of templates should be put? or do I have to chenge the script in order to do a for-each on "grandfather" nodes and then examine the two inner nodes ("father" and "mother") to check if the conditions hold and rebuild them accordingly to the changes I want to apply?

I hope I was clear enough

Best,
Eastvan
 
Old December 21st, 2012, 08:03 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Please provide a well-formed input sample with proper nesting and the corresponding output you want to create. Do you want to output the full document with some changes for each grandchild or only a subtree?
Also if the posted XSLT uses a variable "$document" show us where and how it is being initialized.
Currently I think your problem can be solved by passing the child triggering the change as a parameter (perhaps tunnel parameter) to the templates, then you should be able to check that in the template for mother.
But that is more of a guess as to what you need, I am currently not sure I have understood what the problem is, due to lack of details of the input structure and the result you want.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old December 21st, 2012, 09:18 AM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
Please provide a well-formed input sample with proper nesting and the corresponding output you want to create. Do you want to output the full document with some changes for each grandchild or only a subtree?
Also if the posted XSLT uses a variable "$document" show us where and how it is being initialized.
Currently I think your problem can be solved by passing the child triggering the change as a parameter (perhaps tunnel parameter) to the templates, then you should be able to check that in the template for mother.
But that is more of a guess as to what you need, I am currently not sure I have understood what the problem is, due to lack of details of the input structure and the result you want.
Dear Martin, thanks for your reply.

The input code I posted is just a toy example. It was only necessary to show that I want to copy the original file except for two small changes. One on a grandson node under the father, and one under the corresponding mother's grandson.

The $document variable contains the whole tree. Anyway the real question is: is it possible to output the result of xsl:apply-templates in a specific place of the output file? and not in the same place where it is used (current location identified by the match attribute of the xsl:template in which xsl:apply-templates is nested)

the result I obtain is the following

Code:
<grandfather>
<father>
<child/>
<grandson>Apply CHANGES HERE changes to mother's child</grandson>
</father> <mother/>
<child/>
<grandson/>
</mother>
</grandfather>
Instead of the desired:

Code:
<grandfather>
<father>
<child/>
<grandson>Apply CHANGES HERE </grandson>
</father> <mother/>
<child/>
<grandson>changes to mother's child</grandson>
</mother>
</grandfather>
 
Old December 21st, 2012, 10:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I haven't tried to understand your problem (you really haven't explained it at all clearly), but the answer to this question

Anyway the real question is: is it possible to output the result of xsl:apply-templates in a specific place of the output file?

is "no". If the structure of your input and output trees are different, your logic should follow the structure of the output, not the input. In effect you have random access to the source tree, but only serial append access to the result tree.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 21st, 2012, 10:52 AM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thank you for your answer. It replies to my answer!

Sorry if I was unclear. I am really in a rush and I didn't have time to better revise the message and express more clearly my thoughts.

Best,
EastvanAxon





Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Several Paths maotulua XML 2 March 13th, 2010 07:23 PM
Writing paths in Mysql MarkH2 MySQL 1 May 13th, 2008 08:53 AM
not all code paths return a value galua SQL Language 11 January 20th, 2008 09:36 PM
paths table FT BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 December 12th, 2006 11:50 AM
Filesystemobject and UNC paths dlfisher1972 Pro VB 6 0 January 5th, 2006 01:44 PM





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