2

I am struggling with xslt from the past 2 days, owing to my starter status.My requirement is that given any input XML file ,I want the output to be a list of all the XPaths of all the tags in order in which they appear in the original XML document(parent, then parent,parents Attributes list/child, parent/child/childOFchild and so forth). THe XSLT should not be specific to any single XMl schema. It should work for any XML file, which is a valid one.

Ex:

If the Input XML Is :

<v1:Root>
<v1:UserID>test</v1:UserID>
<v1:Destination>test</v1:Destination>
<v1:entity name="entiTyName">
<v11:attribute name="entiTyName"/>
<v11:attribute name="entiTyName"/>
<v11:attribute name="entiTyName"/>
<v11:filter type="entiTyName">
<v11:condition attribute="entiTyName" operator="eq" value="{FB8D669E-D090-E011-8F43-0050568E222C}"/>
<v11:condition attribute="entiTyName" operator="eq" value="1"/>
</v11:filter>
<v11:filter type="or">
<v11:filter type="or">
<v11:filter type="and">
<v11:filter type="and">
<v11:condition attribute="cir_customerissuecode" operator="not-like" value="03%"/>
</v11:filter>
</v11:filter>
</v11:filter>
</v11:filter>
</v1:entity>
</v1:Root>

I want my output to be :

/v1:Root/v1:UserID
/v1:Root/v1:Destination
/v1:Root/v1:entity/@name
/v1:Root/v1:entity/v11:attribute
/v1:Root/v1:entity/v11:attribute/@name
/v1:Root/v1:entity/v11:attribute[2]
/v1:Root/v1:entity/v11:attribute[2]/@name
/v1:Root/v1:entity/v11:attribute[3]
/v1:Root/v1:entity/v11:attribute[3]/@name
/v1:Root/v1:entity/v11:filter/@type
/v1:Root/v1:entity/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter/v11:condition/@attribute
/v1:Root/v1:entity/v11:filter/v11:condition/@operator
/v1:Root/v1:entity/v11:filter/v11:condition/@value
/v1:Root/v1:entity/v11:filter/v11:condition[2]
/v1:Root/v1:entity/v11:filter/v11:condition[2]/@attribute
/v1:Root/v1:entity/v11:filter/v11:condition[2]/@operator
/v1:Root/v1:entity/v11:filter/v11:condition[2]/@value
/v1:Root/v1:entity/v11:filter[2]/v11:filter/@type
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/@type
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/@type
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition/@attribute
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition/@operator
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition/@value
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/@type
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition/@attribute
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition/@operator
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition/@value
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]/@attribute
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]/@operator
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]/@value

So, it is basically all the XPath of each element ,then the Xpath of the elements Attributes.

I have an XSLT with me, which is like this:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="no" />
    <xsl:template match="*[not(child::*)]">
        <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="concat('/', name())" />
            <xsl:if test="count(preceding-sibling::*[name() = name(current())]) != 0">
                <xsl:value-of
                    select="concat('[', count(preceding-sibling::*[name() = name(current())]) + 1, ']')" />
            </xsl:if>
        </xsl:for-each>

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

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

</xsl:stylesheet>

THe output which gets Produced does not cater to complex tags and also the tag's attributes in the resulting Xpath list :(.

Kindly help me in fixing this xslt to produce the output as mentioned above.

THe present output from the above XSLT is like this :

/v1:Root/v1:UserID
/v1:Root/v1:Destination
/v1:Root/v1:entity/v11:attribute
/v1:Root/v1:entity/v11:attribute[2]
/v1:Root/v1:entity/v11:attribute[3]
/v1:Root/v1:entity/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter/v11:condition[2]
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]
/v1:Root/v1:entity/v11:filter[2]/v11:filter[2]/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter[2]/v11:filter[2]/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter[2]/v11:filter[2]/v11:condition[2]
/v1:Root/v1:entity/v11:filter[2]/v11:filter[2]/v11:filter[2]/v11:condition[3]

1 Answers1

1

I think there's a discrepancy between your sample input and output, in that the output describes a filter element with two conditions that's not in the source XML. At any rate, I believe this works:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="no" />
  <!-- Handle attributes -->
  <xsl:template match="@*">
    <xsl:apply-templates select="ancestor-or-self::*" mode="buildPath" />
    <xsl:value-of select="concat('/@', name())"/>
    <xsl:text>&#xA;</xsl:text>
  </xsl:template>

  <!-- Handle non-leaf elements (just pass processing downwards) -->
  <xsl:template match="*[@* and *]">
    <xsl:apply-templates select="@* | *" />
  </xsl:template>

  <!-- Handle leaf elements -->
  <xsl:template match="*[not(*)]">
    <xsl:apply-templates select="ancestor-or-self::*" mode="buildPath" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:apply-templates select="@*" />
  </xsl:template>

  <!-- Outputs a path segment for the matched element: '/' + name() + [ordinalPredicate > 1] -->
  <xsl:template match="*" mode="buildPath">
    <xsl:value-of select="concat('/', name())" />
    <xsl:variable name="sameNameSiblings" select="preceding-sibling::*[name() = name(current())]" />
    <xsl:if test="$sameNameSiblings">
      <xsl:value-of select="concat('[', count($sameNameSiblings) + 1, ']')" />
    </xsl:if>
  </xsl:template>

  <!-- Ignore text -->
  <xsl:template match="text()" />
</xsl:stylesheet>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • You Sir, made my day. Thank you for Introducing me to XSLT and the solution to my issue within an hour. Thank you very very much, and you are right ,THere is definetly a difference between the INput and output when I mentioned there. It was a copy paste mistake. :( – user1969264 Jan 11 '13 at 09:16