Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2008, 09:37 PM
Registered User
 
Join Date: Nov 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default avoiding duplications

How do I display the name() nodes of author, title, place, and extra just once using XPath expression?

I tried using something like <xsl:value-of select="/*/*/*name()"/> but then it outputs all the author,title, place and extra element nodes found in an xml document.

sample xml document
Code:
<?xml version="1.0"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<rss>
   <channel>
    <item>
        <author>John</author>
        <title>Earthquake</title>
        <place>Japan</place>
    </item>
    <item>
        <author>Mike</author>
        <title>Microsoft Visual Sutdio 2008 now available</title>
                <extra>....</extra>
        <place>United States</place>
    </item>
    <item>
        <author>Jean</author>
        <title>Flood</title>
        <place>England</place>
    </item>
    <item>
        <author>Matt</author>
        <title>Silent Night</title>
        <place>California</place>
    </item>
    <item>
        <author>James</author>
        <title>Fire</title>
        <place>Rome</place>
        </item>
   </channel>
</rss>
 
Old January 5th, 2008, 06:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XPath 2.0: distinct-values(//*/name())

In XPath 1.0 - it can't be done. You want to return a set of strings, and there's no such data type in XPath 1.0.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 5th, 2008, 07:43 AM
Registered User
 
Join Date: Nov 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for replying Michael.

Do we need to use "Saxon XSLT application" in order to be able to use XPath 2.0? In fact.. I think XPath 2.0 doesn't seem to be supported in most applications yet, because it was published last Jan 2007 - please correct me if I'm wrong.

Quote:
quote:Originally posted by mhkay
 In XPath 2.0: distinct-values(//*/name())

In XPath 1.0 - it can't be done. You want to return a set of strings, and there's no such data type in XPath 1.0.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 5th, 2008, 08:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're right, most free-standing XPath implementations still support only XPath 1.0. However, support for 2.0 is becoming more common, for example it's available in Stylus Studio, Oxygen, and XML Spy. If you're using a product that doesn't support it yet, consider switching, or asking your vendor to get a move on.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 5th, 2008, 09:41 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

There is not a single XPath 1.0 expression that gives you the unique names but an XSLT 1.0 stylesheet can nevertheless extract them using a grouping approach, here is a stylesheet that does that:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="text"/>

  <xsl:key name="by-name" match="*/*/*/*" use="name()"/>

  <xsl:template match="/">
    <xsl:for-each select="*/*/*/*[generate-id() = generate-id(key('by-name', name())[1])]">
      <xsl:value-of select="name()"/>
      <xsl:if test="position() != last()">
        <xsl:value-of select="', '"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
 
Old January 5th, 2008, 04:29 PM
Registered User
 
Join Date: Nov 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Martin. - I completely forgot about the capabilities of XSL.






Similar Threads
Thread Thread Starter Forum Replies Last Post
avoiding a <div> to go under the frame nerssi Javascript 2 April 7th, 2007 07:19 AM
Avoiding Double bookings via 'reserved' status davidosullivan PHP Databases 0 March 30th, 2006 10:44 AM
Avoiding multiple Relationships Marden Lee SQL Language 4 February 16th, 2005 08:30 AM
Avoiding Duplicates fixitman Access 1 April 27th, 2004 10:43 AM
Avoiding Displaying duplicate data saleaf10 Classic ASP Databases 1 November 26th, 2003 10:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.