XSLT 2.0 XPATH intersect

断了今生、忘了曾经 提交于 2019-12-13 20:28:05

问题


Under XSLT 2.0, I am trying to deploy intersect to use an xsl:variable in an XPATH expression. Files at https://xsltfiddle.liberty-development.net/gWmuiK6/1 (problem below at lines 175-192 in the xslt).

I declare a variable:

<xsl:variable name="app-sources" select="tei:del[@rend='expunctus'] | 
    tei:gap | 
    tei:sic | 
    tei:supplied[@reason='added'] | 
    tei:surplus[@reason='repeated' or @reason='surplus']  |
    tei:unclear"></xsl:variable>

And I am using that variable here with intersect, and it does not output the expected result:

 <xsl:template match="*[. intersect $app-sources]">
    <xsl:choose>
        <xsl:when test="self::tei:del[@rend='expunctus']">
            [<xsl:text>EXPUNCTUS</xsl:text>]<xsl:apply-templates/><sup> 
           <xsl:number count="*[. intersect $app-sources]" format="a" level="any"/></sup>
        </xsl:when>
    </xsl:choose>
 </xsl:template>

Expected result in HTML[EXPUNCTUS]quondam<sup>b</sup> quandam (around line 465 in the HTML output).

But if I substitute *[. intersect $app-sources] with the original XPATH, it works fine:

 <xsl:template match="tei:del[@rend='expunctus'] | 
    tei:gap | 
    tei:sic | 
    tei:supplied[@reason='added'] | 
    tei:surplus[@reason='repeated' or @reason='surplus']  |
    tei:unclear">
    <xsl:choose>
        <xsl:when test="self::tei:del[@rend='expunctus']">
            [<xsl:text>EXPUNCTUS</xsl:text>]<xsl:apply-templates/><sup> 
           <xsl:number count="tei:del[@rend='expunctus'] | 
                                 tei:gap | 
                                 tei:sic | 
                                 tei:supplied[@reason='added'] | 
                                 tei:surplus[@reason='repeated' or @reason='surplus']  |
                                 tei:unclear" format="a" level="any"/></sup>
        </xsl:when>
    </xsl:choose>
 </xsl:template>

Curiously, intersect works perfectly fine in <xsl:template match="*[. intersect $footnote-sources]" mode="build_footnotes"> just below the above code.


回答1:


If you declare a global variable with

<xsl:variable name="app-sources" select="tei:del[@rend='expunctus'] | 
    tei:gap | 
    tei:sic | 
    tei:supplied[@reason='added'] | 
    tei:surplus[@reason='repeated' or @reason='surplus']  |
    tei:unclear"></xsl:variable>

all your paths like tei:del are used to select with the primary input document node as the context node and your primary input document has a TEI root element but certainly no del or gap or any of the other elements as child nodes of the document node. So you will need to make sure you use paths like //tei:del or //dei:gap to select any descendants elements of the used name of the primary input document.

Of course as you are also using temporary documents created by modes it might be that for that variable you don't want to select descendants of the primary input document but rather ones from a temporary result you have in another variable, so in that case you would need to use e.g. $foo//tei:gap in your path expressions selecting the nodes bound to the app-sources variable.



来源:https://stackoverflow.com/questions/52891964/xslt-2-0-xpath-intersect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!