I'm working on an xslt which will be used to translate a set of xmls files, a sample input xml is something like :-
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="A12">
<bookattribute name="abc" price="200" />
<bookattribute name="xyz" price="300" />
<bookattribute name="pqr" price="400" />
<bookattribute name="lnz" price="500" />
</book>
<book id="B12">
<bookattribute name="cpz" price="300" />
<bookattribute name="rts" price="800" />
</book>
<novel id="AA12">
<bookattribute name="yps" price="200" />
<bookattribute name="udv" price="600" />
</novel>
<!-- book node with id=AA12 may or may not be present in the xml -->
<book id="AA12">
<bookattribute name="abc" price="200" />
<bookattribute name="aps" price="600" />
</book>
</bookstore>
i want to transform it by creating copy of node "book" with attribute id="A12" based on condition that , if node book with attribute id=AA12 does not exist, then create copy of node "book"(attribute id="A12") and in the copy change id to "AA12" (thus its copy of node with change of attribute value), but if node with book(id="AA12") already exists in the xml then add those child nodes of book(id=A12) which are not present in "book"(attribute id="AA12"), Here I want to add only those child nodes(with bookattribute "name" as the key ) which are not present in book@id=AA12) , for example if child node bookattribute name="abc" is already present in book@id=AA12 then it should not be added again to it, further their may be certain other element nodes like novel or ebook which may also have attribute id="AA12" these nodes must be copied as it is.
so output, in case (input file above contains node "book"(attribute id="AA12") )
<bookstore>
<book id="A12">
<bookattribute name="abc" price="200" />
<bookattribute name="xyz" price="300" />
<bookattribute name="pqr" price="400" />
<bookattribute name="lnz" price="500" />
</book>
<book id="B12">
<bookattribute name="cpz" price="300" />
<bookattribute name="rts" price="800" />
</book>
<novel id="AA12">
<bookattribute name="yps" price="200" />
<bookattribute name="udv" price="600" />
</novel>
<book id="AA12">
<bookattribute name=aps price=600 />
<bookattribute name="abc" price="200" />
<bookattribute name="xyz" price="300" />
<bookattribute name="pqr" price="400" />
<bookattribute name="lnz" price="500" />
</book>
</bookstore>
output , in case (input file does not contain node "book"(attribute id="AA12") )
<bookstore>
<book id="A12">
<bookattribute name="abc" price="200" />
<bookattribute name="xyz" price="300" />
<bookattribute name="pqr" price="400" />
<bookattribute name="lnz" price="500" />
</book>
<book id="B12">
<bookattribute name="cpz" price="300" />
<bookattribute name="rts" price="800" />
</book>
<novel id="AA12">
<bookattribute name="yps" price="200" />
<bookattribute name="udv" price="600" />
</novel>
<book id="AA12">
<bookattribute name="abc" price="200" />
<bookattribute name="xyz" price="300" />
<bookattribute name="pqr" price="400" />
<bookattribute name="lnz" price="500" />
</book>
</bookstore>
I have been able to create xslt as below which creates copy with changed attributes as required , but im unable to work out addition of child nodes in case node book with id="AA12" exists, any pointers would be helpful
my xslt:-
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="comment()|processing-instruction()">
<xsl:comment>
<xsl:value-of select="."/>
</xsl:comment>
</xsl:template>
<xsl:template match="book[@id='A12']">
<xsl:copy>
<xsl:attribute name="id">A12</xsl:attribute>
<xsl:apply-templates />
</xsl:copy>
<xsl:copy>
<xsl:attribute name="id">AA12</xsl:attribute>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
xslt-1.0
andxslt-2.0
- please tag the question with only one of them. – Mathias Müller