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