[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to extract value of variable number of attribu

Subject: Re: How to extract value of variable number of attributes in an XML t ag through XSL?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 16 Nov 2004 16:02:14 +0000
xsl value of variable
Hi Anoop,

> If I could loop through the list of attributes in these, it could be
> easy to implement so that I could get to table tag and loop through
> its attributes list and add more attributes also. But I don't know
> how to do that. Because I don't know for sure what attributes may be
> present each time, I can't hardcode each attribute's value in final
> output. Please suggest if there could be an implementation for what
> I am trying to do.

Use the step "@*" to access all the attributes on the context node.
For example:

<xsl:template match="table">
  <table class="...">
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
  </table>
</xsl:template>

will copy all the attributes on the <table> element to the new <table>
element. Or, if you need to add defaults when the value is missing,
you can do, for example:

<xsl:template match="table">
  <table class="...">
    <xsl:apply-templates select="@*" mode="copy" />
    <xsl:apply-templates />
  </table>
</xsl:template>

<xsl:template match="@*" mode="copy">
  <xsl:attribute name="{name(.)}">
    <xsl:choose>
      <xsl:when test="string(.)">
        <xsl:value-of select="." />
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="." mode="default" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>
</xsl:template>

<xsl:template match="align" mode="default">left</xsl:template>
<xsl:template match="bgcolor" mode="default">white</xsl:template>
<xsl:template match="colspan" mode="default">1</xsl:template>
...

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.