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 29th, 2010, 11:25 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default Need to determine uniquiness among several attributes

Hi,

I'm using saxon 9.0.0.6J from a Cygwin bash shell on WinXP. I have Oxygen XML v.11, so I have saxon EE in there, but I don't think I can access it from a command line, so for now, I'll be using saxon 9

Here's my problem:

I have a structure where a particular element has 3 attributes
something like:

PHP Code:
<root>
  <
elem1>
    <
elem2 attr1="val1" attr2="val2" attr3="val3">
      <
elem3/>
    </
elem2>
  </
elem1>
  <
elem1>
    <
elem2 attr1="val1" attr2="val2" attr3="val3">
      <
elem3/>
    </
elem2>
   </
elem1>
   <
elem1>
    <
elem2 attr1="val1" attr2="val2" attr3="val3">
      <
elem3/>
    </
elem2>
  </
elem1>
...
</
root
and so on..

what I want to do is be sure there is only one unique combination of values for attr1, attr2 and attr3 across all the possible elem2s in the structure.

It looks like a job for groups, but I don't see how to create the select statement to gather the items I'm looking for.

Thanks,

- m
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old January 29th, 2010, 11:44 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

<xsl:for-each-group select="//elem2" group-by="concat(@attr1,@attr2,@attr2)">

This will give you groups of elements with the came attribute set. THen you can use current-group()[1] to get the first element, and look at its attributes.

Sam
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
mphare (January 29th, 2010)
 
Old January 29th, 2010, 11:53 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You could group by some concatenation of the attribute values, as already suggested, although I would use some separator character that is not part of the attribute values, then you could count the number of groups, if it is one then you have the same attribute values on all elements:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:param name="sep" select="'|'"/>
  
  <xsl:template match="/">
    <xsl:variable name="groups">
      <xsl:for-each-group select="root/elem1/elem2" group-by="string-join((@attr1, @attr2, @attr3), $sep)">
        <group/>
      </xsl:for-each-group>
    </xsl:variable>
    <xsl:variable name="count" select="count($groups/group)"/>
    <!-- now check $count eq 1 here -->
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
mphare (January 29th, 2010)
 
Old January 29th, 2010, 03:02 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Thanks for the help.
For what I needed the concat() worked perfectly.
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare





Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine if numeric Scootterp Access VBA 4 March 2nd, 2006 08:44 AM
Determine Credentials bmumph C# 2 November 1st, 2005 12:18 PM
Determine OS adman Beginning VB 6 2 January 5th, 2004 02:26 AM
Need Help: Can't determine cause of error xgbnow Visual C++ 3 September 22nd, 2003 05:00 PM
Determine if something has already been selected harpua Classic ASP Basics 1 June 13th, 2003 01:02 AM





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