简体   繁体   中英

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)

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()" /> )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM