Adding child/parent links to a flat XML file in XSLT -
i have flat xml document. nodes contain information indicating if parent or child node , in sequential order child nodes follow parent nodes. need xslt file convert input xml xml file child nodes have link parent nodes.
input xml file:
<items> <item type="a"> <value id="11111" relation="parent"></value> </item> <item type="b"> <value id="22222" relation="child"></value> </item> <item type="b"> <value id="33333" relation="child"></value> </item> <item type="a"> <value id="44444" relation="parent"></value> </item> <item type="b"> <value id="55555" relation="child"></value> </item> <item type="a"> <value id="66666" relation="parent"></value> </item> <item type="a"> <value id="77777" relation="parent"></value> </item> <item type="b"> <value id="88888" relation="child"></value> </item> <item type="b"> <value id="99999" relation="child"></value> </item> <item type="b"> <value id="00000" relation="child"></value> </item> </items>
i want convert into:
<items> <item> <itemtype>a</itemtype> <itemid>11111</itemid> <itemrelationship>parent</itemrelationship> <itemparentid /> </item> <item> <itemtype>b</itemtype> <itemid>22222</itemid> <itemrelationship>child</itemrelationship> <itemparentid>insert parent id here</itemparentid> </item> <item> <itemtype>b</itemtype> <itemid>33333</itemid> <itemrelationship>child</itemrelationship> <itemparentid>insert parent id here</itemparentid> </item> <item> <itemtype>a</itemtype> <itemid>44444</itemid> <itemrelationship>parent</itemrelationship> <itemparentid /> </item> <item> <itemtype>b</itemtype> <itemid>55555</itemid> <itemrelationship>child</itemrelationship> <itemparentid>insert parent id here</itemparentid> </item> <item> <itemtype>a</itemtype> <itemid>66666</itemid> <itemrelationship>parent</itemrelationship> <itemparentid /> </item> <item> <itemtype>a</itemtype> <itemid>77777</itemid> <itemrelationship>parent</itemrelationship> <itemparentid /> </item> <item> <itemtype>b</itemtype> <itemid>88888</itemid> <itemrelationship>child</itemrelationship> <itemparentid>insert parent id here</itemparentid> </item> <item> <itemtype>b</itemtype> <itemid>99999</itemid> <itemrelationship>child</itemrelationship> <itemparentid>insert parent id here</itemparentid> </item> <item> <itemtype>b</itemtype> <itemid>00000</itemid> <itemrelationship>child</itemrelationship> <itemparentid>insert parent id here</itemparentid> </item> </items>
where “insert parent id here” replaced child’s node parent id.
here current xslt file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <items> <xsl:for-each select="items/item"> <item> <xsl:variable name = "vcurrentitemtype" select ="@type"/> <xsl:choose> <xsl:when test = "$vcurrentitemtype = 'a'"> <xsl:call-template name="drawparent"> <xsl:with-param name = "pcurrentitem" select = "current()"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:call-template name="drawchild"> <xsl:with-param name = "pcurrentitem" select = "current()"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </item> </xsl:for-each> </items> </xsl:template> <xsl:template name = "drawparent"> <xsl:param name = "pcurrentitem" /> <itemtype> <xsl:value-of select ="$pcurrentitem/@type"/> </itemtype> <itemid> <xsl:value-of select ="$pcurrentitem/value/@id"/> </itemid> <itemrelationship> <xsl:value-of select ="$pcurrentitem/value/@relation"/> </itemrelationship> <itemparentid></itemparentid> </xsl:template> <xsl:template name = "drawchild"> <xsl:param name = "pcurrentitem" /> <itemtype> <xsl:value-of select ="$pcurrentitem/@type"/> </itemtype> <itemid> <xsl:value-of select ="$pcurrentitem/value/@id"/> </itemid> <itemrelationship> <xsl:value-of select ="$pcurrentitem/value/@relation"/> </itemrelationship> <itemparentid>insert parent id here</itemparentid> </xsl:template> </xsl:stylesheet>
the expression want parent id this....
<xsl:value-of select="$pcurrentitem/preceding-sibling::item[@type='a'][1]/value/@id" />
however, may consider simplifying xslt this, avoid code repetition....
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output method="xml" indent="yes" /> <xsl:template match="item"> <item> <itemtype> <xsl:value-of select="@type"/> </itemtype> <itemid> <xsl:value-of select="value/@id"/> </itemid> <itemrelationship> <xsl:value-of select="value/@relation"/> </itemrelationship> <itemparentid> <xsl:if test="@type='b'"> <xsl:value-of select="preceding-sibling::item[@type='a'][1]/value/@id" /> </xsl:if> </itemparentid> </item> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment