[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] extracting all elem names of an attribute


Subject: Re: [xsl] extracting all elem names of an attribute
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 23 Jan 2004 07:25:12 -0500

At 2004-01-23 02:04 -0500, Saverio Perugini wrote:
How best is the following text extracted
from the following xml, possibly using keys
or some other xslt constructs?
...
The basic approach involves:

Gee, that sounds like a good approach to me:


- using keys to get a unique list of possible values for the @category attribute

<xsl:key name="cats" match="*[@category]" use="@category"/>


- iterating over these values

<xsl:for-each select="//*[@category] [generate-id(.)=generate-id(key('cats',@category)[1])]">

- printing each followed by a colon

<xsl:value-of select="@category"/>: <xsl:text/>


- getting a unique list of all xml element names which contain this particular attribute value
- iterating over these values

<xsl:for-each select="key('cats',@category)"> <xsl:if test="generate-id(.)= generate-id(key('cats',@category)[name(.)=name(current())][1])">

printing each

<xsl:value-of select="name(.)"/>


printing an endline

<xsl:text> </xsl:text>

Many thanks

For what? You already understand the solution! :{)}


...................... Ken

p.s. you have a typo in your XML data, unless you like peepers on your pizza

t:\ftemp>type saverio.xml
<db>
   <large category="size">
      <thin category="crust">
         <olives category="topping"/>
         <pepperoni category="topping"/>
      </thin>
      <pan category="crust">
         <pepperoni category="topping"/>
      </pan>
   </large>
   <medium category="size">
      <pan category="crust">
         <pepperoni category="topping"/>
         <olives category="topping"/>
         <peppers category="topping"/>
      </pan>
   </medium>
   <small category="size">
      <thin category="crust">
         <olives category="topping"/>
         <pepperoni category="topping"/>
         <peepers category="topping"/>
      </thin>
      <pan category="crust">
         <pepperoni category="topping"/>
         <olives category="topping"/>
         <peepers category="topping"/>
      </pan>
   </small>
</db>

t:\ftemp>type saverio.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:key name="cats" match="*[@category]" use="@category"/>

<xsl:template match="/">
  <xsl:for-each select="//*[@category]
                       [generate-id(.)=generate-id(key('cats',@category)[1])]">
    <xsl:value-of select="@category"/>: <xsl:text/>
    <xsl:for-each select="key('cats',@category)">
      <xsl:if test="generate-id(.)=
               generate-id(key('cats',@category)[name(.)=name(current())][1])">
        <xsl:if test="generate-id(.)!=
                      generate-id(key('cats',@category)[1])">, </xsl:if>
        <xsl:value-of select="name(.)"/>
      </xsl:if>
    </xsl:for-each>
      <xsl:text>
</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>saxon saverio.xml saverio.xsl
size: large, medium, small
crust: thin, pan
topping: olives, pepperoni, peppers, peepers

t:\ftemp>


-- Public courses: sign up for one or both soon to reserve your seat! Each week: Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO Washington, DC: 2004-03-15 San Francisco, CA: 2004-03-22 Hong Kong, China: 2004-05-17 Bremen, Germany: 2004-05-24 World-wide on-site corporate, government & user group XML training

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


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




Current Thread
Keywords