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

Re: Need help with XSL and XPath

Subject: Re: Need help with XSL and XPath
From: "Jay Bryant" <jay@xxxxxxxxxxxx>
Date: Mon, 22 May 2006 18:28:29 -0500
Re:  Need help with XSL and XPath
Here's one way to do it:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <out>
      <xsl:for-each select="data/element[not(number = following::number)]">
        <xsl:sort select="number"/>
        <xsl:variable name="this-number" select="number"/>
        <entry number="{$this-number}">
          <xsl:copy-of select="/data/element[number = $this-number]/name"/>
        </entry>
      </xsl:for-each>
    </out>
  </xsl:template>

</xsl:stylesheet>

I tested against the following data with Saxon and Xalan:

<data>
  <element>
    <name>Oscar</name>
    <number>1</number>
  </element>
  <element>
    <name>Mike</name>
    <number>1</number>
  </element>
  <element>
    <name>Bob</name>
    <number>2</number>
  </element>
  <element>
    <name>George</name>
    <number>2</number>
  </element>
  <element>
    <name>Tom</name>
    <number>3</number>
  </element>
  <element>
    <name>Dick</name>
    <number>3</number>
  </element>
  <element>
    <name>Harry</name>
    <number>3</number>
  </element>
</data>

It produced the following output:

<?xml version="1.0" encoding="UTF-8"?>
<out>
  <entry number="1">
    <name>Oscar</name>
    <name>Mike</name>
  </entry>
  <entry number="2">
    <name>Bob</name>
    <name>George</name>
  </entry>
  <entry number="3">
    <name>Tom</name>
    <name>Dick</name>
    <name>Harry</name>
  </entry>
</out>

I'm sure other ways exist.

Jay Bryant
Bryant Communication Services

----- Original Message ----- 
From: "Lassi Seppdld" <lsseppal@xxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Monday, May 22, 2006 5:48 PM
Subject:  Need help with XSL and XPath


> Here's a simplified description of my problem:
>
> I've got an XML document with multiple entries of <element> as shown
> below. The <name>s are unique, but the <number> can be same for
> mulpitle entries of <element>. The XML document isn't sorted in anyway.
>
> <element>
> <name>name</name>
> <number>1</number>
> </element>
>
> Now I need the XSL stylesheet to print out something like this:
>
> number 1
> name1
> name4
> name7
>
> number 25
> name2
> name3
>
> number 100
> name5
> name6
>
> So each "number X" is only printed once and then all the <element>s
> that have the number X in the <number> tags are printed below that.
> Then the next biggest number is printed and so on.
>
> Any advice?
>
> --
> Lassi

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.