2
    <BookList>
    <Book>
    <History>
    <Type>history</Type>
    <Prize>123</Prize>
    <Publication>``
    <Name>YEAP1</Name>
    </Publication>
    <RNumber Type="VolumeNumber">11111</RNumber>
    <RNumber Type="SupplementNumber">123456</RNumber>
    </History>
    <chemistry>
    <Type>chemistry</Type>
    <Prize>333</Prize>
    <Publication>
    <Name>YEAP</Name>
    </Publication>
    <RNumber Type="VolumeNumber">11111</RNumber>
    <RNumber Type="SupplementNumber">45454</RNumber>
    </chemistry>
                      ......

    </Book>
    </BookList>

There are duplicate VolumnNumber 11111. How to check duplicate VolumnNumber in BoolList xml using xslt . please help on this

3
  • Possible duplicate with: stackoverflow.com/questions/157705/… Commented Apr 18, 2012 at 18:56
  • Are you just trying to check to see if there is a duplicate? What is the expected output supposed to look like? (ie, an html page that displays which volumes are duplicates?) Commented Apr 18, 2012 at 19:05
  • need to check is there any duplicate volumnnumber exist in xml or not Commented Apr 18, 2012 at 19:07

1 Answer 1

2

I. This can be found using a single XPath expression:

    false()
   or
    /*/*/*/RNumber
           [@Type='VolumeNumber'
          and
            . = ../preceding-sibling::*
                   /RNumber[@Type='VolumeNumber']
            ]

Here is a complete XSLT transformation that evaluates this XPath expression and outputs the result of this evaluation:

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

 <xsl:template match="/">
  Duplicate volume numbers exist: <xsl:text/>

  <xsl:value-of select=
   "false()
   or
    /*/*/*/RNumber
           [@Type='VolumeNumber'
          and
            . = ../preceding-sibling::*
                   /RNumber[@Type='VolumeNumber']
            ]
   "/>

 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<BookList>
    <Book>
        <History>
            <Type>history</Type>
            <Prize>123</Prize>
            <Publication>``     
                <Name>YEAP1</Name>
            </Publication>
            <RNumber Type="VolumeNumber">11111</RNumber>
            <RNumber Type="SupplementNumber">123456</RNumber>
        </History>
        <chemistry>
            <Type>chemistry</Type>
            <Prize>333</Prize>
            <Publication>
                <Name>YEAP</Name>
            </Publication>
            <RNumber Type="VolumeNumber">11111</RNumber>
            <RNumber Type="SupplementNumber">45454</RNumber>
        </chemistry>
        ......      
    </Book>
</BookList>

the wanted, correct result is produced:

  Duplicate volume numbers exist: true

II. Solution using keys (generally more efficient):

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

  <xsl:key name="kVolNum" match="RNumber[@Type='VolumeNumber']"
          use="."/>

 <xsl:template match="/">
  Duplicate volume numbers exist: <xsl:text/>

  <xsl:value-of select=
   "false()
   or
    /*/*/*/RNumber
            [@Type='VolumeNumber'
           and
             key('kVolNum',.)[2]
            ]"/>

 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the same XML document (above), the same correct result is produced:

  Duplicate volume numbers exist: true
Sign up to request clarification or add additional context in comments.

2 Comments

Long time since 2003/04/05 on XSL List - Just playing after a decade - If I wanted to check if there were any duplicates of <string>com.text1.text2</string> thru an XML file at any level, will this still work or should I post a fresh question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.