Sequential attribute values.

Questions about XML that are not covered by the other forums should go here.
William
Posts: 42
Joined: Sun Jul 15, 2012 12:26 pm
Location: London

Sequential attribute values.

Post 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
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Sequential attribute values.

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
William
Posts: 42
Joined: Sun Jul 15, 2012 12:26 pm
Location: London

Re: Sequential attribute values.

Post by William »

Excellent Adrian. Thank you very much for your time and effort, I really do appreciate it.

--
William
Post Reply