I don't have muck knowledge in writing xsl transformations. I have an xml like this
<details>
<order>
<id>testID</id>
<name>testName</name>
</order>
<contract>
<id>1234</id>
<date>01-01-2015</date>
<contactAddress>
<name>ABC</name>
<email>[email protected]</email>
</contactAddress>
</contract>
</details>
I want to transform this xml where each node having child nodes will form an item and will also maintain the relationship using itemID and relatedItemID
Like this
<items>
<item>
<name>order</name>
<itemID>1</itemID>
<characteristics>
<characterisctic>
<name>id</name>
<value>testID</value>
</characterisctic>
<characterisctic>
<name>name</name>
<value>testName</value>
</characterisctic>
</characteristics>
</item>
<item>
<name>contract</name>
<itemID>2</itemID>
<characteristics>
<characterisctic>
<name>id</name>
<value>1234</value>
</characterisctic>
<characterisctic>
<name>date</name>
<value>01-01-2015</value>
</characterisctic>
</characteristics>
</item>
<item>
<name>contactAddress</name>
<itemID>3</itemID>
<relatedItemID>2</relatedItemID>
<characteristics>
<characterisctic>
<name>name</name>
<value>ABC</value>
</characterisctic>
<characterisctic>
<name>email</name>
<value>[email protected]</value>
</characterisctic>
</characteristics>
</item>
</items>
For this i have written an xsl like this.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
>
<xsl:variable name="i"></xsl:variable>
<xsl:template match="/">
<items>
<xsl:apply-templates select="//order"/>
<xsl:apply-templates select="//contract"/>
</items>
</xsl:template>
<xsl:template match="*">
<item>
<name><xsl:value-of select="local-name()"/></name>
<itemID><xsl:value-of select="position()" /></itemID>
<characteristics>
<xsl:for-each select="./*">
<xsl:choose>
<xsl:when test="*">
<xsl:apply-templates select="."></xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<characterisctic>
<name> <xsl:value-of select="local-name()"/></name>
<value><xsl:value-of select="."/></value>
</characterisctic>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</characteristics>
</item>
</xsl:template>
</xsl:stylesheet>
But i am not able to figure out how to keep seperate itemID for each item and and maintain the relation using relatedItemID
How to write an xsl for achieving this?
generate-id()function for this. I am not sure I fully understand your purpose here, though: why isordernot "related" todetails?