1

I have a xml like this and I want the attribute which is defined in the "title" using XSL file.

I want to retrieve the value even if I change the elements

  • catalog with books
  • cd with book

XML:

<catalog>
    <cd>
        <title att="abce"  att2="false">Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
</catalog>

I tried this method to get att value

<xsl:variable name="outermostElementName" select="catalog/cd/title/attribute::att"/>

but this is specific to this XML only I want it to be genric

Is there any way?

1
  • Librak: You may be interested to see a more generic, true XSLT solution than the currently accepted answer. Commented Feb 17, 2012 at 16:14

2 Answers 2

1

Given your current xsl:variable, I think you could use one of the following to more generic

<xsl:variable name="outermostElementName" select="catalog/*/title/@att" />

or

<xsl:variable name="outermostElementName" select="//title/@att" />

The first one assumes the root element is always called catalog. The second one does not depend on an ancestor nodes, but will pick up the title element at any level in the XML.

Sign up to request clarification or add additional context in comments.

1 Comment

yes it is helpful but what if there is no catalog,cd and title i meanit is books,book and title...?? is there any method to detect this...
0

The genericity you want to achieve is offered by the XSLT template match patterns.

You can simply have:

<xsl:template match="title/@att">
  <!-- Your processing here  -->
</xsl:template>

and whenever there is an <xsl:apply-templates select="expression"/> instruction such that expression selects a title/@att attribute, the template above will be selected for ptocessing of this attribute node.

This doesn't depend on any other outside context.

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.