Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find missing values in range.

Status
Not open for further replies.

BillMoo

Programmer
Oct 6, 2010
2
GB
Hi,

Given this fragment from an XML document:
Code:
<range>
            <value from="12" to="14" output="400" />
            <value from="15" to="21" output="730" />
            <value from="22" to="28" output="433" />
            <value from="29" to="80" output="230" />
            <value from="83" to="85" output="220" />
            <value from="86" to="122" output="320" />
</range>
I would like to find out what values are missing from a known range. In this case I know the range of the from / to attributes to be 1 to 168 inclusive so I can see simply by looking that I am missing 1 to 11, 81 to 82 and 123 to 168 easy enough, but I want to produce an XSLT transformation that will tell me this. Needless to say I've googled and I can't really come up with a concrete sample so I thought I would ask here.

Is anyone able to help me out here? If not maybe you know of a generic XML/XSLT forum where I could re-post this.

--
Thank you,

Moo.
 
[0] I assume the well-ordering of intervals that some kind of assumptions on the non-overlapping of intervals.

[1] If they are kind of well-ordering in the sense of [0], the following shows you how those non-covered intervals can be exposed, within the xslt 1.0 pure.
[tt]
<xsl:eek:utput method="text" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:param name="m">1</xsl:param>
<xsl:param name="M">168</xsl:param>
<xsl:template match="/root">
<xsl:apply-templates select="value">
<xsl:sort select="@from" data-type="number" order="ascending" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="value">
<xsl:call-template name="proc">
<xsl:with-param name="mproc" select="preceding-sibling::value[1]/@to" />
<xsl:with-param name="Mproc" select="following-sibling::value[1]/@from" />
</xsl:call-template>
</xsl:template>

<xsl:template name="proc">
<xsl:param name="mproc" />
<xsl:param name="Mproc" />
<xsl:choose>
<xsl:when test="not(number($mproc)) and (number(@from) &gt; number($m))">
<xsl:value-of select="concat(number($m),'-',(number(@from) - 1))" />
</xsl:when>
<xsl:when test="not(number($Mproc)) and (number(@to) &lt; number($M))">
<xsl:value-of select="concat((number(@to)+1),'-', number($M))" />
</xsl:when>
<xsl:eek:therwise>
<xsl:choose>
<xsl:when test="(number(@from)-1) &lt; (number($mproc)+1)">
<!-- do nothing -->
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="concat(',',(number($mproc)+1),'-', (number(@from)-1),',')" />
</xsl:eek:therwise>
</xsl:choose>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top