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 7th, 2010, 07:32 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Question How to copy elements to new file

Hi,

I'm using XSLT 2.0 with saxon9.
I would like to seperate my current xml into a few seperate files according to certain element.

Here's my input
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Book version="1.1" Publisher="Wrox">
    <BookDetails NbrOfPages="15" Name="Novel 123">
        <Binding Name="ABC HELLO1" from="1" to="1"/>
        <Binding Name="DEF HELLO2" from="2" to="3"/>
        <Binding Name="GHI HELLO3" from="4" to="10"/>
        <Binding Name="JKL HELLO4" from="11" to="15"/>
        <Page Position="1" Name="page 1" start_writing="2010-02-17T00:17:50"/>
        <Page Position="2" Name="page 2" start_writing="2010-02-17T01:18:20"/>
        <Page Position="3" Name="page 3" start_writing="2010-02-17T01:18:20"/>
        <Page Position="4" Name="page 4" start_writing="2010-02-17T01:18:20"/>
        <Page Position="5" Name="page 5" start_writing="2010-02-17T01:18:20"/>
        <Paper Position="6" Name="page 6" start_writing="2010-02-17T01:18:20"/>
        <Page Position="7" Name="page 7" start_writing="2010-02-17T01:18:20"/>
        <Page Position="8" Name="page 8" start_writing="2010-02-17T01:18:20"/>
        <Page Position="9" Name="page 9" start_writing="2010-02-17T01:18:20"/>
        <Page Position="10" Name="page 10" start_writing="2010-02-17T01:18:20"/>
        <Page Position="11" Name="page 11" start_writing="2010-02-17T01:18:20"/>
        <Paper Position="12" Name="page 12" start_writing="2010-02-17T01:18:20"/>
        <Page Position="13" Name="page 13" start_writing="2010-02-17T01:18:20"/>
        <Page Position="14" Name="page 14" start_writing="2010-02-17T01:18:20"/>    
        <Page Position="15" Name="page 15" start_writing="2010-02-17T01:18:20"/>    
    </BookDetails>
</Book>
I would like to have this result
Code:
ABC.xml
<?xml version="1.0" encoding="UTF-8"?>
<Book version="1.1" Publisher="Wrox">
    <BookDetails NbrOfPages="1" Name="Novel 123">
        <Binding Name="ABC HELLO1" from="1" to="1"/>
        <Page Position="1" Name="page 1"/>
    </BookDetails>
</Book>

DEF.xml
<?xml version="1.0" encoding="UTF-8"?>
<Book version="1.1" Publisher="Wrox">
    <BookDetails NbrOfPages="2" Name="Novel 123">
        <Binding Name="DEF HELLO2" from="1" to="2"/>
        <Page Position="1" Name="page 2"/>
        <Page Position="2" Name="page 3"/>
    </BookDetails>
</Book>
GHI.xml
<?xml version="1.0" encoding="UTF-8"?>
<Book version="1.1" Publisher="Wrox">
    <BookDetails NbrOfPages="7" Name="Novel 123">
        <Binding Name="GHI HELLO3" from="1" to="7"/>
        <Page Position="1" Name="page 4"/>
        <Page Position="2" Name="page 5"/>
        <Paper Position="3" Name="page 6"/>
        <Page Position="4" Name="page 7"/>
        <Page Position="5" Name="page 8"/>
        <Page Position="6" Name="page 9"/>
        <Page Position="7" Name="page 10"/>     
    </BookDetails>
</Book>

JKL.xml
<?xml version="1.0" encoding="UTF-8"?>
<Book version="1.1" Publisher="Wrox">
    <BookDetails NbrOfPages="5" Name="Novel 123">
        <Binding Name="JKL HELLO4" from="1" to="5"/> 
        <Page Position="1" Name="page 11"/>
        <Paper Position="2" Name="page 12"/>
        <Page Position="3" Name="page 13"/>
        <Page Position="4" Name="page 14"/>    
        <Page Position="5" Name="page 15"/>    
    </BookDetails>
</Book>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions" 
    xmlns:f="http://local/functions"
    exclude-result-prefixes="xs f" version="2.0">
    <xsl:output method="xml" indent="yes"/>
 
     <xsl:template match="/">
            <xsl:apply-templates/>
     </xsl:template> 
    
    <xsl:template match="Book" name="book">
        <xsl:copy>
            <xsl:copy-of select="@*"/>  
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="BookDetails">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:for-each select="//Binding">
                <xsl:variable name="name"><xsl:value-of select="@Name"/></xsl:variable>
                <xsl:variable name="filename"><xsl:value-of select="fn:substring(@Name,1,3)"/></xsl:variable>
                <xsl:result-document href="{$filename}.xml">
                   <!-- I'm stuck here -->
                 </xsl:result-document>       
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Page|Paper">
        <!-- 
        <xsl:if test="xs:integer(@Position) &gt;= 1 and xs:integer(@Position) &lt;= 121"> 
            <xsl:copy-of select=".|node()"/>
        </xsl:if>
        -->
    </xsl:template>
</xsl:stylesheet>
I understand that because I have the process of creating the file is inside the "for-each", the current node will be <Binding> so I tried to get parent element and copy the node to the file that created. But it didn't work, it keep start the xml file element as <Binding>.

Could someone please help me how can I achieve my result out of my xml.

Thanks.
Reply With Quote
  #2 (permalink)  
Old January 7th, 2010, 08:00 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Personally I'd wrap the whole lot around the result-document rather than trying to write the result-document at the bottom and then go back up.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes"/>
   
   <xsl:template match="/">
         <xsl:for-each select="/Book/BookDetails/Binding">
               <xsl:variable name="N" select="substring-before(@Name,' ')"/>
               <xsl:result-document href="{$N}.xml">
                  <xsl:apply-templates select="/Book">
               <xsl:with-param name="binding" select="$N"/>
                  </xsl:apply-templates>
               </xsl:result-document>
           </xsl:for-each>
       </xsl:template>

   <xsl:template match="node() | @*">
      <xsl:param name="binding" select="''"/>
      <xsl:copy>
         <xsl:apply-templates select="node() | @*">
            <xsl:with-param name="binding" select="$binding"/>
         </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>
       
   <xsl:template match="BookDetails">
           <xsl:param name="binding"/>
         <BookDetails>
         <xsl:variable name="B" select="Binding[contains(@Name, $binding)]"/>
         <xsl:apply-templates select="@*"/>
         <xsl:apply-templates select="$B">
            <xsl:with-param name="binding" select="$binding"/>
         </xsl:apply-templates>
         <xsl:apply-templates select="Page[@Position &gt;= $B/@from and @Position &lt;= $B/@to]">
            <xsl:with-param name="binding" select="$binding"/>
         </xsl:apply-templates>
         </BookDetails>
    </xsl:template>

</xsl:stylesheet>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #3 (permalink)  
Old January 7th, 2010, 08:06 AM
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

First, change this

<xsl:for-each select="//Binding">
<xsl:variable name="name"><xsl:value-of select="@Name"/></xsl:variable>
<xsl:variable name="filename"><xsl:value-of select="fn:substring(@Name,1,3)"/></xsl:variable>
<xsl:result-document href="{$filename}.xml">
<!-- I'm stuck here -->
</xsl:result-document>
</xsl:for-each>


to this:

<xsl:for-each select="Binding">
<xsl:variable name="name" select="@Name"/>
<xsl:variable name="filename" select="fn:substring(@Name,1,3)"/></xsl:variable>
<xsl:result-document href="{$filename}.xml">
<!-- I'm stuck here -->
</xsl:result-document>
</xsl:for-each>

Your code isn't wrong here, it's just that it can be improved. In particular, using xsl:value-of within xsl:variable, and unnecessary use of //, are both habits to avoid.

Now the substance. As far as I can see, within the xsl:result document you want something like:

Code:
<xsl:apply-templates select="../(Page|Paper)[@Position = xs:integer(current()/@from) to xs:integer(current()/@to)]"
and the rest should be straightforward
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #4 (permalink)  
Old January 9th, 2010, 01:03 PM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Question How to copy elements to new file

Thanks for the improvement tips :)

It works, but how can I change the position attribute accordingly without having to copy the attribute one by one?

I also tried to remove the start_writing attribute just like in W3 documentation
Code:
<xsl:template match="Page|Paper">
         <xsl:copy>
         <xsl:copy-of select="@* |node()"/>
                <xsl:apply-templates select="@*"/>
         </xsl:copy>
</xsl:template> 

 <xsl:template match="start_writing"/>
but then the result is become something like

Code:
<Page Position="1" Name="page 1" start_writing="2010-02-17T00:17:50"/>1page 12010-02-17T00:17:50
Any idea what did I do wrong?
Reply With Quote
  #5 (permalink)  
Old January 9th, 2010, 01:22 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

xsl:copy-of select="@*" and xsl:apply-templates select="@*" in one template? What do you want to achieve with that?
And if you want to prevent processing the 'start_writing' attribute then you need
Code:
<xsl:template match="@start_writing"/>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #6 (permalink)  
Old January 9th, 2010, 02:09 PM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I'm trying to copy the node without the start_writing attribute.

I've tried your suggestion to put '@' in front of start_writing. but the result is still the same.

I've also tried
Code:
<xsl:template match="Page|Paper">
         <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>  
         </xsl:copy>
</xsl:template> 

 <xsl:template match="@start_writing"/>
the result become something like
Code:
<Page>1page 12010-02-17T00:17:50</Page>
it doesn't create the attribute name.
Reply With Quote
  #7 (permalink)  
Old January 10th, 2010, 08:35 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to copy all attributes except the 'start_writing' attribute then you can simply use (with XSLT 2.0)
Code:
<xsl:template match="Page | Paper">
   <xsl:copy>
     <xsl:copy-of select="@* except @start_writing"/>
   </xsl:copy>
</xsl:template>
That sample does not do anything with child nodes as I am not sure what you want to do with them, you can of course add an <xsl:copy-of select="node()"/> if you want to copy them as well.
If you want to transform some attributes, copy some, but don't want to copy the 'start_writing' attribute then you need the
Code:
<xsl:template match="Page | Paper">
   <xsl:copy>
      <xsl:apply-templates select="@* | node()/">
   </xsl:copy>
</xsl:template>
approach, then you need to add
Code:
<xsl:template match="@start_writing"/>
but you need to also ensure that other attributes are copied by adding
Code:
<xsl:template match="@*>
   <xsl:copy/>
</xsl:template>
as otherwise the default template for attributes is used which copies the value of the attribute to the output. That is why one of your attempts puts all those attribute values in the element content.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
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
copy-of select= not returning all elements jay_c XSLT 8 August 8th, 2009 01:38 PM
XSL file can't find elements in XML data? Mateo1041 XSLT 2 September 18th, 2008 09:37 AM
Macro to copy data from one file to a 2nd file chadpodsednik Excel VBA 1 October 29th, 2004 10:40 AM
How to copy file? eapsokha Classic ASP Databases 2 November 14th, 2003 11:44 AM
How to replace elements(from existed XML file)? hbcontract XML 1 October 30th, 2003 05:49 AM



All times are GMT -4. The time now is 11:19 PM.


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