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,