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 March 14th, 2012, 12:12 PM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default XSL Multiple copies of the original file with minor changes

Hi everybody,

I'm new to XSL and I need some help. My goal is to generate multiple .xml files which differs only by the content of one tag. Here an example:

The original .xml file has the form:

<root>
<a>
[...]
</a>
<b>
[...]
</b>
<c>
[...]
</c>
<d>
<t1>
</t1>
<t2>
</t2>
<t3>
</t3>
</d>
</root>

and I would like to generate 3 different files:

<root>
<a>
[...]
</a>
<b>
[...]
</b>
<c>
[...]
</c>
<d>
<NEW>
</NEW>
<t2>
</t2>
<t3>
</t3>
</d>
</root>

<root>
<a>
[...]
</a>
<b>
[...]
</b>
<c>
[...]
</c>
<d>
<t1>
</t1>
<NEW>
</NEW>
<t3>
</t3>
</d>
</root>

<root>
<a>
[...]
</a>
<b>
[...]
</b>
<c>
[...]
</c>
<d>
<t1>
</t1>
<t2>
</t2>
<NEW>
</NEW>
</d>
</root>


have you got any ideas (I'm using xsl 2.0)?

Best,
Axon
 
Old March 14th, 2012, 01:35 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Something like this, perhaps:

Code:
<xsl:template match="/">
  <xsl:result-document href="out1.xml">
    <xsl:apply-templates mode="m1"/>
  </xsl:result-document>
  <xsl:result-document href="out2.xml">
    <xsl:apply-templates mode="m2"/>
  </xsl:result-document>
  <xsl:result-document href="out3.xml">
    <xsl:apply-templates mode="m3"/>
  </xsl:result-document>
</xsl:template>

<xsl:template match="*" mode="#all">
  <xsl:copy><xsl:apply-templates mode="#current"/></xsl:copy>
</xsl:template>

<xsl:template match="t1" mode="m1">
  <NEW/>
</xsl:template>

<xsl:template match="t2" mode="m2">
  <NEW/>
</xsl:template>

<xsl:template match="t3" mode="m3">
  <NEW/>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 14th, 2012, 02:16 PM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default

thanks a lot Michael for your answer!

For the specific example it will probably work. Sadly I'm dealing with more complex xml sources, i.e. t1, t2, t3 are indeed all equal (<t1>=<t2>=<t3>=<t>) and I don't know in advance how many occurrences of <t> are there.

For this reason I cannot do different matches like:

<xsl:template match="t1" mode="m1">
<NEW/>
</xsl:template>

Here the new correct input file:
<root>
<a>[...]</a>
<b>[...]</b>
<c>[...]</c>
<d>
[...]
<t></t>
<t></t>
<t></t>
[...]
</d>
</root>

and the expected result files:
1)
<root>
<a>[...]</a>
<b>[...]</b>
<c>[...]</c>
<d>
[...]
<new/>
<t></t>
<t></t>
[...]
</d>
</root>

2)
<root>
<a>[...]</a>
<b>[...]</b>
<c>[...]</c>
<d>
[...]
<t></t>
<new/>
<t></t>
[...]
</d>
</root>

3)
<root>
<a>[...]</a>
<b>[...]</b>
<c>[...]</c>
<d>
[...]
<t></t>
<t></t>
<new/>
[...]
</d>
</root>


I'm so sorry if I made you waste time with a non-general example.
Thanks again for your reply.

Best,
Axon
 
Old March 15th, 2012, 05:30 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Try changing

Code:
<xsl:template match="t1" mode="m1">
  <NEW/>
</xsl:template>
to

Code:
<xsl:template match="t[1]" mode="m1">
  <NEW/>
</xsl:template>
etc.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 15th, 2012, 09:31 AM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thanks for your suggestions. I was able to solve the issue in a slightly different way, but your hints has been super useful!!!

Here the solution I designed (the XPaths are a bit more complex w.r.t the example and I also needed to perform a check on an attribute ^_^):

<xsl:template match="/">
<xsl:for-each select="a/b/c/d/e/f[attribute::name = 'blablabla']">
<xsl:variable name="currNode" select="position()"/>
<xsl:variable name="uri" select="concat('multi_',$currNode,.xml')"/>
<xsl:result-document href="{$uri}">
<xsl:apply-templates select="../../../../../../*">
<xsl:with-param name="counter" select="$currNode"/>
</xsl:apply-templates>
</xsl:result-document>
</xsl:for-each>
</xsl:template>

<xsl:template match="node( ) | @*">
<xsl:param name="counter"/>
<xsl:copy>
<xsl:apply-templates select="@* | node( )">
<xsl:with-param name="counter" select="$counter"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="d/e/f">
<xsl:param name="counter"/>
<xsl:choose>
<xsl:when test="self::node()[attribute::name = 'blablabla'] and position()=$counter*2"><NEW name="NEW"/></xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node( )">
<xsl:with-param name="counter" select="$counter"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


The use of <xsl:apply-templates select="../../../../../../*"> was necessary in order to get the whole original file copied, otherwise, only the changed part of the tree were displayed in the result files. I know that probably there are better solutions (with a cleaner and simplier style) but this seems to work and for now I'll keep it :D

Another question: Do you know why I had to use "position()=$counter*2"? By using only position()=$counter when $counter was equal 1 no changes occurred, and when it was equal 2 only the first occurrence of <f name='blablabla'> where changed.

Thanks again for your help!!!
Axon

Last edited by EastvanAxon; March 15th, 2012 at 09:34 AM.. Reason: Forgot a question about position()=$counter*2
 
Old March 15th, 2012, 09:58 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The "../../../../../../*" could probably just be changed to "/*".
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use multiple template in the xsl file? dhrumil XSLT 2 April 1st, 2010 04:04 AM
Unable to print multiple copies for crystal report in vb.net. amoldwani .NET Framework 3.5 0 February 15th, 2010 11:02 AM
Unable to print multiple copies for crystal report in vb.net. amoldwani .NET Framework 3.5 0 February 15th, 2010 11:00 AM
How can I deploy multiple copies of the CMS app. on the same web server pbourget BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 7 February 16th, 2009 03:42 PM
xsl:copy copies everywhere [email protected] XSLT 1 November 11th, 2005 07:44 AM





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