Erik
Erik

Reputation: 944

XSLT 2.0 function not executing

I have this template:

<xsl:template match="opf:manifest">
    <xsl:copy>
    <xsl:analyze-string select="unparsed-text('Styles/fonts.css')" regex='url\(\"(.*[^\"]*)\"\)'>
        <xsl:matching-substring>
            <xsl:element name="opf:item">
                <xsl:attribute name="href" select="regex-group(1)"/> 
                <xsl:attribute name="media-type">application/vnd.ms-opentype
                </xsl:attribute>
                <xsl:attribute name="id" select="generate-id()"/> 
            </xsl:element>
        </xsl:matching-substring>
    </xsl:analyze-string>
    <xsl:apply-templates select="document('index.xml')//opf/(jpeg | scripts)"/>
    <xsl:apply-templates select="document('index.xml')/numberGroup/entry/file"/>
    </xsl:copy>
</xsl:template>

The regex-group function does what it is supposed to do. However, the generate-id() does not. XMLSPY Debugger stumbles: "error in XPATH 2.0 expression (not a node item)." What do I do wrong? (btw: generate-id(.) does the same)

Upvotes: 0

Views: 48

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

Inside analyze-string the current context item (.) is a string (the current matching or non-matching substring), not a node, so you can't pass it to generate-id. If what you want is the generated ID of the node that the current template matched then you need to cache it in a variable outside the analyze-string and then use that variable with generate-id:

<xsl:variable name="dot" select="." />
<xsl:analyze-string select="unparsed-text('Styles/fonts.css')" regex='url\(\"(.*[^\"]*)\"\)'>
    <xsl:matching-substring>
        <!-- ... -->
        <xsl:attribute name="id" select="generate-id($dot)"/>

(or indeed just cache the ID itself <xsl:variable name="theId" select="generate-id()" />)

Upvotes: 2

Related Questions