What is the time complexity of the index access for the following XPath expression in XSLT?
<xsl:value-of select="User[2]/username"/>
- O(log(n))
- O(1) or
- O(n)
I have a sorted xml-file with thousands of users which looks like this:
<Users>
<User>
<idPerson>460</idPerson>
<username>a_aker01</username>
</User>
<User>
<idPerson>677</idPerson>
<username>a_aker02</username>
</User>
<User>
<idPerson>1844</idPerson>
<username>a_aker03</username>
</User>
<User>
<idPerson>2373</idPerson>
<username>a_aker04</username>
</User>
</Users>
I am thinking of writing a binary search function in XSLT 2.0 (requiring a fast index access) for a faster search, because
<xsl:variable name="targetId" select="2373" />
<xsl:value-of select="User[idPerson=$targetId]/username"/>
is too slow for my needs. Does it perform a linear search?
The time-complexity of
/Users/User[2]is implementation-specific. Most likely it will be an O(n) linear search, but perhaps there is an implementation out there that is smart enough to do it in O(1) under ideal conditions.However, why don’t you just use
xsl:keyinstead of creating a binary search function? (This will work with XSLT 1.0 too.)