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 June 30th, 2006, 10:59 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default unique children detection

I'm new into XSLT and I made an Xpath expression to find out if an element with a specific attribute has siblings with the same attribute values.
This is what I made and want to know if this is the right way do do it?

example xml:
<elementname>
    <ns1:langstring xml:lang="nl">Dutch description</ns1:langstring>
    <ns1:langstring xml:lang="nl">Another Dutch description</ns1:langstring>
    <ns1:langstring xml:lang="en">English description</ns1:langstring>
</elementname>

example xsl:
       <xsl:template match="ns1:langstring">
<xsl:if test="following-sibling::*[@xml:lang = current()/@xml:lang]">
           there is more than 1 element with the @xml:lang value <xsl:value-of select="@xml:lang" /> in element <xsl:value-of select="../name()" />
       </xsl:if>
    </xsl:template>
 
Old June 30th, 2006, 11:20 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can use any grouping method to find groups of elements with matching values (use <xsl:for-each-group> in XSLT 2.0, or Muenchian grouping in 1.0 - see http://www.jenitennison.com/xslt/grouping). Then display the grouping key for any group whose size is >1.

In 2.0:
<xsl:template match="elementname">
<xsl:for-each-group select="*" group-by="@xml:lang">
  <xsl:if test="count(current-group()) gt 1">
    There is >1 with key <xsl:value-of select="current-grouping-key()"/>
  </xsl:if>
</xsl:for-each-group>
</xsl:template>

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
Cash Drawer Detection imcityhunter VB How-To 2 April 24th, 2007 05:20 PM
browser detection Adam H-W Javascript 0 January 20th, 2007 09:55 AM
flash detection catchrohith Classic ASP Basics 0 November 18th, 2006 01:29 AM
File extension detection RichardOrmiston ASP.NET 1.0 and 1.1 Basics 2 November 2nd, 2005 11:46 AM
Infinite Loop Detection jtdang77 Classic ASP Basics 0 October 14th, 2003 05:32 PM





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