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 March 15th, 2007, 05:17 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selecting unique set of elements

Hi there,

I am not sure why I am not getting the desired result. Example of my source xml is

<root>
  <a/>
  <b/>
  <a/>
  <d/>
  <b/>
  <c/>
</root>

my desired result should be like

<root>
  <a/>
  <b/>
  <d/>
  <c/>
</root>

but for some reason i am getting everything from the source. my xslt looks like this

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

      <xsl:param name="argument"/>
      <xsl:template match="/root">

            <xsl:variable name="unique" select="*[not(preceding-sibling::*= name())]"/>

            <root>
                  <xsl:for-each select="$unique">
                        <xsl:variable name="counter" select="count(preceding-sibling::*)"/>
                        <xsl:element name="{name()}">
                              <xsl:value-of select="$counter"/>
                        </xsl:element>
                  </xsl:for-each>
            </root>
      </xsl:template>
</xsl:stylesheet>

I think it has to do with my predicate where preceding-sibling::* is returning a node set and I am comparing it to an element name. but i am not sure to extract the element name from preceding-sibling::* so that i can do the compare

thanks

 
Old March 15th, 2007, 05:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're comparing the content of one element with the name of another.

You're relying on XSLT's "existential equals" where A=B is true if any node in A is equal to any node in B. But this only works where you are comparing the values of nodes, you can't use it to compare other properties such as their names.

Assuming you're stuck with XSLT 1.0, you can use logic along the lines:

<xsl:if test="not(preceding-sibling::*[name() = name(current())])">

You need to use this to test each node in turn, because of the reliance on current() you can't easily select all the nodes that satisfy this condition.

Better, move to XSLT 2.0 which makes it all very easy with xsl:for-each-group.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
unique attribute names for form elements fishmonkey XSLT 3 March 16th, 2008 07:46 PM
help in selecting elements spandit XSLT 1 April 9th, 2007 07:27 PM
Selecting unique value of an attribute and render vinaura XSLT 4 December 18th, 2006 04:35 PM
Selecting elements up until a certain one Frode XSLT 5 January 19th, 2006 01:22 PM
selecting certain xml elements fogofogo XML 2 December 5th, 2005 07:33 AM





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