markg
markg

Reputation: 434

XSLT 2.0 case-insensitive matching with key()

I'm checking for the existence of a string in an @id attribute in an XML file. I don't care about the case, only that the letters of the strings match. So "myid_5" can equal "mYiD_5", "MYID_5" and so on. I'm using <xsl:key> and key(), and apparently I can't use the lower-case() function with keys to normalize the @id's found by the key. Can I? Or something similar?

Example of the XML file ($lookup-file below):

 <root>
   <p id="A41_YrlyDedHdr">Blah</p>
   <p id="A42_YrlyDed15">Blah</p>
 </root>

The key for $lookup-file:

<xsl:key name="p-id" match="/root/p" use="@id"/>

The template:

<xsl:template match="fig">
  <xsl:variable name="id" select="lower-case(@id)"/>
  <xsl:choose>
    <!-- ?? Can force to lowercase below?? -->
    <xsl:when test="exists(key('p-id', $id, $lookup-file))">
      <!-- do something with <fig> -->
    </xsl:when>
    <xsl:otherwise/>
  </xsl:choose>
</xsl:template>

It seems that a collation on the key might make it case-insensitive, but I don't know what collation to use.

Upvotes: 0

Views: 977

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

apparently I can't use the lower-case() function with keys

Why not? You could define the key as:

<xsl:key name="p-id" match="/root/p" use="lower-case(@id)"/>

then use it as:

<xsl:when test="exists(key('p-id', lower-case(@id), $lookup-file))">

It seems that a collation on the key might make it case-insensitive

That's an interesting idea, but I believe the implementation would be processor-dependent. So in Saxon you could do:

<xsl:key name="p-id" match="/root/p" use="@id" collation="http://saxon.sf.net/collation?ignore-case=yes"/>

http://www.saxonica.com/documentation/index.html#!extensibility/config-extend/collation/implementing-collation

Upvotes: 1

Related Questions