I have the following XML structure:
<file>
<root1>
<object1 id="abc" info="blah"/>
<object1 id="def" info="blah blah"/>
</root1>
<root2>
<object2 id="abc" x="10" y="20"/>
<object2 id="def" x="30" y="40""/>
</root2>
</file>
and I want to transform (merge) it into the following structure:
<file>
<root>
<object id="abc" info="blah" x="10" y="20"/>
<object id="def" info="blah blah" x="30" y="40"/>
</root>
</file>
We can assume that there are no duplicated nodes nor attributes, for the same id.
Currently, I'm looping throughout the collection of object1
using <xsl:for-each ...>
, but I can't figure out how to make this work:
<xsl:for-each select="file/root1/object1">
<object>
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="info"><xsl:value-of select="@info"/></xsl:attribute>
<xsl:attribute name="x">???</xsl:attribute>
<xsl:attribute name="y">???</xsl:attribute>
</object>
</xsl:for-each>
i.e. I need to use the @id
of the currently selected <object1>
as input for an xpath query on <object2>
, inside an attribute of <object>
.
I've seen this, this, this, this, this and this but they're all a bit different and I couldn't see how I use it in my case.