Gururaj CV
Gururaj CV

Reputation: 15

Does any one know how to print or get full path from XML using xslt?

Sample Xml:

Below is the sample Xml

<root name="Product">
    <nodeOne name="Model Filter"/>  
    <Folder name="Eye Care Hierarchies">
        <nodeOne name="Eye Care Subcat-Manufacturer"/>
        <nodeOne name="Eye Care Subcat-Segment"/>
    </Folder>
    <Folder name="Mega Hierarchies">
        <nodeOne name="Mega Hierarchies"/>
    </Folder>
</root>

Expected result:

Its added with a new attribute named "fullpath"

<root name="Product">
    <nodeOne name="Model Filter" fullpath="Product"/>   
    <Folder name="Eye Care Hierarchies" fullpath="Product">
        <nodeOne name="Eye Care Subcat-Manufacturer" fullpath="Product.Eye Care Hierarchies"/>
        <nodeOne name="Eye Care Subcat-Segment" fullpath="Product.Eye Care Hierarchies"/>
    </Folder>
    <Folder name="Mega Hierarchies" fullpath="Product">
        <nodeOne name="Mega Hierarchies" fullpath="Product.Mega Hierarchies"/>
    </Folder>
</root>

Upvotes: 1

Views: 448

Answers (1)

Michael
Michael

Reputation: 9044

The following XSLT provides pretty close to what you're after. It could be optimised a bit, but it's late and bedtime.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="utf-8" />

<xsl:template match="*">
    <xsl:copy>
            <xsl:apply-templates select="@*" mode="copy" />
            <xsl:apply-templates select="*" mode="copy">
                <xsl:with-param name="path" select="@name" />
            </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="copy">
    <xsl:param name="path" />
    <xsl:copy select=".">
        <xsl:attribute name="fullpath"><xsl:value-of select="$path" />
    </xsl:attribute>
    <xsl:apply-templates select="@*" mode="copy"/>
    <xsl:apply-templates select="*" mode="copy">
        <xsl:with-param name="path" select="concat($path, '.', @name)" />
    </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*" mode="copy">
        <xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute>
</xsl:template>

Upvotes: 1

Related Questions