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

Re: Template matching attribute

Subject: Re: Template matching attribute
From: Peter Davis <pdavis152@xxxxxxxxx>
Date: Fri, 6 Sep 2002 00:30:30 -0700
xslt matching attributes with namespaces
On Thursday 05 September 2002 22:36, Kalyan Kumar Mudumbai wrote:
> I am merging two files, and I have an element in
> file 1 like this.
>
> <Currency Unit="DOL">50</Currency>
>
> and in file 2 I have an element:
>
> <Unit key="DOL" val="$"/>
>
> In the output I should get it as:
>
> <Currency Unit="$">50</Currency>

I would normally suggest that you use <xsl:key> to easily look up the 
key/value pairs, but it is difficult (although not impossible) to adapt that 
to this usage since the keys and values are stored in another document, and 
since it sounds like the key names aren't known beforehand.  So, I'll try to 
suggest something else.  It's untested:

[I left out the 'xsl:' prefix on all of the elements below, but the meaning 
stays the same.]

The first step is to load the second document and store it in a variable:

<variable name="keys" select="document('keys.xml')"/>

And since you're copying everything but the attributes verbatim, the second 
step is to write a basic identity template:

<template match="node()">
  <copy>
    <apply-templates select="@*"/>
    <apply-templates/>
  </copy>
</template>

Now, you can write one or more templates to copy the attributes with their 
modified values.  If you know the list of attributes that need to be modified 
beforehand, you can write them all out manually like this:

<template match="@Unit">
  <attribute name="Unit">
    <!-- with this usage, you could easily use <xsl:key> here instead,
      bearing in mind that you'd have to use <xsl:for-each> to switch the
      current document; there are a number of other threads on how to use the
      key() function with multiple input documents -->
    <value-of select="$keys//Unit[@key = current()]/@val"/>
  </attribute>
</template>

If you don't know the attributes and elements beforehand, you can use an 
uglier but more general approach:

<template match="@*">
  <!-- give the new attribute the name and namespace as the original -->
  <attribute name="{name()}" namespace-uri="{namespace-uri()}">
    <!-- look up the new value in $keys, in the element with the same
       name and namespace as the attribute and with @key equal to the
       value of the attribute. -->
    <value-of select="$keys//*[local-name() = local-name(current())]
                      [namespace-uri() = namespace-uri(current())]
                      [@key = current()]/@val"/>
  </attribute>
</template>

Hope that helps!

-- 
Peter Davis

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.