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 2nd, 2011, 06:24 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default Two transformations on same input data

Hi all,

I have some pretty flat input xml, for example:
Code:
<root>
   <person1name>Bob<person1name>
   <person2name>Fred<person2name>
   <person3name>Harry<person3name>
</root>
I have an xsl file which contains a template called "tidyup", this improves the structure, e.g.:
Code:
<root>
   <people>
      <person id="1">
         <name>Bob</name>
      </person>
      <person id="2">
         <name>Fred</name>
      </person>
      ..and so on
I then have a "main" xsl file which is called by a server, this is the stylesheet used to transform the xml from one format into another.
My aim is for it to first call the tidyup stylesheet to improve the input xml structure, and then transform the tidied xml into a completely new format.

So far my "main" stylesheet looks like:
Code:
...
<xsl:include href="tidyup.xsl" />

<xsl:template match="*">
   <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
   </xsl:copy>
</xsl:template>

<xsl:template match="root">
   <xsl:call-template name="tidyup" />
</xsl:template>
	
<xsl:template match="/">
   <newxml>
      <xsl:value-of select="//name[.='Fred']" />
   </newxml>
</xsl:template>
As you can see from above, I am trying to output any "name" nodes with the value Fred.
However, the second template (matching to "/") seems to be trying to run against the original xml, so I would need to look for "//person2name" instead.

Please could someone offer any advice as to how I can get the second template (matching to "/") to run against the tidied xml (i.e. the result of the first template, matching to "root" and calling "tidyup").

Any help appreciated, thanks.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old December 2nd, 2011, 06:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Splitting a complex transformation into a sequence of simpler transformations is an excellent design technique (usually called 'pipelines') so you're on the right track.

There are two ways of implementing a pipeline of transformations: single-stylesheeet and multi-stylesheet. With a single-stylesheet approach (your current approach), you need to use modes, one mode for each phase of processing. Template rules for the first mode should say mode="phase1" and use apply-templates mode="phase1", etc for subsequent phases. Capture the result of each processing phase in a variable (can be local or global, for example

Code:
<xsl:variable name="phase1output">
  <xsl:apply-templates select="/" mode="phase1">
</xsl:variable>
<xsl:template match="/">
  <xsl:apply-templates select="$phase1output" mode="phase2"/>
</xsl:template>
With a two-stylesheet approach you need some external logic to cause the second stylesheet to be applied to the output of the first (or in Saxon you could use saxon:next-in-chain in xsl:output). The external logic might be your own Java code, or Ant, or XProc, or a shell-script, or a framework such as Coccoon or Orbeon.

The multi-stylesheet approach needs more coordination but gives you much more flexibility in reusing code and constructing pipelines dynamically.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 20th, 2011, 11:00 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thanks for the info Michael, I haven't had chance to try this out yet so when I get around to it I may have some more questions, but this should point me in the right direction.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old April 30th, 2012, 06:38 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default First Template Transform Issue

Hi again, I have finally revisited this and am having an issue with the initial "phase1" transform.
My stylesheet is now along the lines of:
Code:
<xsl:include href="tidyup.xslt" />
	
<xsl:variable name="tidyup_output">
	<xsl:apply-templates select="/" mode="untidy" />
</xsl:variable>
	
<xsl:template match="*">
	<xsl:apply-templates select="$tidyup_output" mode="tidied" />
</xsl:template>
The output XML was blank, so I tried changing the select attribute of the tidyup_output variable apply-templates command to "/|*", which then just output the XML tag values, and not the tags as well.

I have change the content of the "xsl:template match.." template to just output the value of the variable "tidyup_output" to see exactly what it is doing, and get the same results as above, so it appears the issue lies with the initial tidyup transformation, which is handled in an included stylesheet.

The included "tidyup.xslt" stylesheet has a template command:
Code:
<xsl:template match="*|/" mode="untidy">
Previously on the tidyup stylesheet, I just named the template "tidyup", and called this in the main stylesheet, and the XML tags were copied along with the data inside them.

This must be a difference between apply-templates and call-template but I can't see how to copy the XML tags as well as data - any help would be appreciated on this.

Thanks,
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old April 30th, 2012, 06:59 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, you haven't supplied enough code to allow a reader to find your bug.

When you say it outputs "the tag values", I assume you mean that it outputs the text nodes only - that is, the content occurring between the tags, which would more correctly be described as the non-tag values. The normal reason for this is that none of your template rules have matched any element nodes, and the most common reason for this is that you haven't taken namespaces into account.

A good diagnostic technique at this point is to display the value of the variable $tidyup_output (using xsl:message and xsl:copy-of, or using your preferred XSLT debugger) so that you can determine which phase of processing contains the error.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 3rd, 2012, 05:25 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi Michael,

I went back through your earlier post and over my code again and have managed to get it working now. I think my template match attributes were incorrect but I have sorted it now.

Thanks for your help with this.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.





Similar Threads
Thread Thread Starter Forum Replies Last Post
More about Graphics Transformations jercook Visual Basic 2010 General Discussion 2 May 11th, 2011 05:24 PM
sequential transformations alexan XSLT 0 July 24th, 2008 11:59 PM
XSLProcessor asynchronous transformations Vx BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 September 5th, 2006 12:41 PM
DTS transformations Amorous SQL Server DTS 0 January 18th, 2006 04:54 PM
XSL Transformations scottrudy VB.NET 0 July 4th, 2003 11:59 AM





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