xpointer.idref — Extract IDREF from an XPointer


The xpointer.idref template returns the ID portion of an XPointer which is a pointer to an ID within the current document, or the empty string if it is not.

In other words, xpointer.idref returns foo when passed either #foo or #xpointer(id('foo')), otherwise it returns the empty string.

<xsl:template name="xpointer.idref">
  <xsl:param name="xpointer">http://...</xsl:param>
    <xsl:when test="starts-with($xpointer, '#xpointer(id(')">
      <xsl:variable name="rest" select="substring-after($xpointer, '#xpointer(id(')"></xsl:variable>
      <xsl:variable name="quote" select="substring($rest, 1, 1)"></xsl:variable>
      <xsl:value-of select="substring-before(substring-after($xpointer, $quote), $quote)"></xsl:value-of>
    <xsl:when test="starts-with($xpointer, '#')">
      <xsl:value-of select="substring-after($xpointer, '#')"></xsl:value-of>
    <!-- otherwise it's a pointer to some other document -->