0

I would like to print path of element and attributes if any along with values using XSLT. e.g

XML : 

    <root>
       <node attr='abc' module='try'>       
          <subnode>Roshan</subnode>
          <subnode>Chetan</subnode>
          <subnode>Jennifer</subnode>
       </node>
    </root>

    Output : 
    /root##
    /root/node##
    /root/node/@attr##abc
    /root/node/@module##try
    /root/node/subnode[1]##Roshan
    /root/node/subnode[2]##Chetan
    /root/node/subnode[3]##Jennifer

I am trying with below snippet, but could only print path of element and it's value

<xsl:template match="*">
    <xsl:for-each select="ancestor-or-self::*">
        <xsl:value-of select="concat('/',local-name())" />
        <xsl:if
            test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
            <xsl:value-of
                select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')" />
        </xsl:if>
        <!-- <xsl:call-template name="attrData"></xsl:call-template> -->
    </xsl:for-each>
    <xsl:text>##</xsl:text>     
    <xsl:apply-templates select="node()" /> 
</xsl:template>

I am new to XSLT. Please help!!!!

2

1 Answer 1

0

I made the following XSLT and added also the [position] to the output. You can remove that if you need.

This gives this output:

/root[1]
/root[1]/node[1]
/root[1]/node[1]/@attr[1]##abc
/root[1]/node[1]/@module[1]##try
/root[1]/node[1]/subnode[1]##Roshan
/root[1]/node[1]/subnode[2]##Chetan
/root[1]/node[1]/subnode[3]##Jennifer

With this XSLT. With the two output template you can choose how to print the Xpath.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns=""  version="2.0">
    <xsl:output method="text" encoding="utf-8" />


    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:call-template name="generateXPath">     
            <xsl:with-param name="previous" select="''"/>            
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="generateXPath">
        <xsl:param name="previous" as="xs:string"/>
        <xsl:variable name="this" select="." as="node()"/>
        <xsl:if test="not(empty(.))">
            <xsl:variable name="thisXPath" select="concat($previous, '/', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']')"></xsl:variable>
            <xsl:apply-templates select="." mode="output">
                <xsl:with-param name="previous" select="$previous"/>
            </xsl:apply-templates>
            <xsl:text>&#10;</xsl:text>
            <xsl:for-each select="*|@*">    
                <xsl:call-template name="generateXPath">        
                    <xsl:with-param name="previous" select="$thisXPath"/>            
                </xsl:call-template>
            </xsl:for-each>  
        </xsl:if>        
    </xsl:template>  

    <xsl:template  match="*" mode="output">
        <xsl:param name="previous" as="xs:string"/>
        <xsl:variable name="this" select="." as="node()"/>
            <xsl:variable name="thisXPath">
                <xsl:value-of select="concat($previous, '/', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']')"></xsl:value-of>
                <xsl:if test="not(*)">
                    <xsl:value-of select="concat('##',text())"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:value-of select="$thisXPath" />    
    </xsl:template> 

    <xsl:template  match="@*" mode="output">
        <xsl:param name="previous" as="xs:string"/>
        <xsl:variable name="this" select="." as="node()"/>
        <xsl:variable name="thisXPath" select="concat($previous, '/@', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']','##',.)"></xsl:variable>
        <xsl:value-of select="$thisXPath" />      
    </xsl:template>


</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.