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 January 10th, 2010, 12:31 PM
Authorized User
 
Join Date: Nov 2007
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Question How to append node to an existing file

Hi,

I'm using XSLT 2.0.

I would like to append a node to an existing file, but I do not want to create another new file.

for example,
FILE1
Code:
<?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>
current node
Code:
        <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"/>
result FILE1
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Book version="1.1" Publisher="Wrox">
    <BookDetails NbrOfPages="7" Name="Novel 123">
        <Binding Name="DEF HELLO2" from="1" to="2"/>
        <Page Position="1" Name="page 2"/>
        <Page Position="2" Name="page 3"/>
        <Page Position="3" Name="page 11"/>
        <Paper Position="4" Name="page 12"/>
        <Page Position="5" Name="page 13"/>
        <Page Position="6" Name="page 14"/>    
        <Page Position="7" Name="page 15"/> 
    </BookDetails>
</Book>
My code snippet
Code:
...

<xsl:when test="doc-available('file1.xml')">
                        
     <!-- file exist then append to file -->
     <xsl:variable name="curfile" select="document('file1.xml')"/>
         <xsl:result-document href="'file1.xml'">

             <Book>
                            
             <xsl:attribute name="Version" select="../../../Book/@Version"/>

             <xsl:attribute name="Provider" select="../../../Book/@Publisher"/>

             <BookDetails NbrOfPages="{xs:integer($curfile/Book/BookDetails/@NbrOfPages)+xs:integer(current()/@from)-xs:integer(current()/@to)+1}" Name="{$curfile/Book/BookDetails/@Name}">

                   <xsl:copy-of select="$curfile/Book/BookDetail/(Paper|Page)"/>
                   <xsl:copy-of select="../(Paper|Page)[@Position=xs:integer(current()/@from) to xs:integer(current()/@to)]"/>
             </BookDetails>
       </xsl:element>

    </xsl:result-document>
</xsl:when>
...
When I tried to write to the same file, of course I will have error "Cannot write to the URI that has already been read".

So how can I write to an existing file and append my new nodes?

Another question is when I appending to a new file I would like to change the "position" accordingly. So if the existing last position is 2 then when I appending the next position will be 3. Any idea how can I do this?

Thanks,
 
Old January 10th, 2010, 12:42 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't think you can read from and write to the same file in one transformation step. You will need to create a result file that is different from the input file and then use tools outside of XSLT to overwrite the input file.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 10th, 2010, 01:27 PM
Authorized User
 
Join Date: Nov 2007
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks :)

I'm using java to transform the XSLT. Can XSLT return the transformed result back into Java?
 
Old January 10th, 2010, 01:38 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't quite understand what you are asking. Saxon is implemented in Java and certainly exposes an API to Java programmers so that you can use Saxon in your Java applications. Michael Kay is better qualified than I to tell how to use it.
You could start by reading http://www.saxonica.com/documentatio...embedding.html
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 10th, 2010, 02:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You could write the result to a file and (if the transformation is successful) rename the file with the name of the original input. Or you could sent the result to a StringWriter (in effect, buffering it in memory), and then (again, only if successful) write the contents of the StringWriter to the file from your Java application.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a node to an existing xml node list. codehelp C# 2008 aka C# 3.0 2 October 12th, 2009 07:41 AM
Append element to existing set fixit XSLT 1 September 23rd, 2009 12:05 PM
append a xml node from one file to another pravind XSLT 5 August 22nd, 2009 09:15 AM
Adding a new Node to existing XML document Lupus81 XML 3 August 4th, 2009 04:57 PM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM





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