Page 1 of 1
Sequential attribute values.
Posted: Thu Jul 19, 2012 6:53 pm
by William
Hello,
Is there an easy way to verify that an attribute's value is sequential from a series of elements and if not what ones are missing?
Assuming that the values started at 1 I could use @id = position() but that doesn't answer the what's missing question.
I would appreciate your thoughts on this.
--
William
Re: Sequential attribute values.
Posted: Wed Jul 25, 2012 4:12 pm
by adrian
Hi,
For XSLT, if the attribute's value range is reasonable, you can simply iterate on each value of the attribute and check if it exists.
e.g.
XML (not ordered):
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item id="3"></item>
<item id="5"></item>
<item id="1"></item>
<item id="7"></item>
<item id="10"></item>
</root>
XSLT (iterates between 1 and the maximum value of the id attribute):
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:template match="/">
<xsl:variable name="root" select="."/>
<xsl:message select="'enter'"/>
<xsl:for-each select="1 to xs:integer(max(//item/@id))">
<xsl:variable name="i" select="."/>
<xsl:if test="not($root//item[@id = $i])">
<xsl:message select="concat('not found: ', $i)"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This will just issue messages for the missing id values:
Code: Select all
[Saxon-PE] not found: 2
[Saxon-PE] not found: 4
[Saxon-PE] not found: 6
[Saxon-PE] not found: 8
[Saxon-PE] not found: 9
Regards,
Adrian
Re: Sequential attribute values.
Posted: Wed Jul 25, 2012 5:11 pm
by William
Excellent Adrian. Thank you very much for your time and effort, I really do appreciate it.
--
William