Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 如何在ID之间选择范围_Xslt_Xslt 2.0 - Fatal编程技术网

Xslt 如何在ID之间选择范围

Xslt 如何在ID之间选择范围,xslt,xslt-2.0,Xslt,Xslt 2.0,请建议如何选择ID之间的范围。例如,如果范围为5-8,则需要6,7个ID。如果图-,则图4图5图6为所需ID XML: XSLT2: 所需结果: 我无法从你当前的XSLT中得出你的逻辑,所以我会考虑一种不同的方法,使用模板来匹配你所需要的各种类型的链接元素。具体地说,对于在文本节点前面或后面带有连字符的链接元素,可以使用单独的链接元素 试试这个XSLT。这就利用intersect函数来获取003-006所需的元素范围 <xsl:stylesheet version="2.0" xmlns:

请建议如何选择ID之间的范围。例如,如果范围为5-8,则需要6,7个ID。如果图-,则图4图5图6为所需ID

XML:

XSLT2:

所需结果:


我无法从你当前的XSLT中得出你的逻辑,所以我会考虑一种不同的方法,使用模板来匹配你所需要的各种类型的链接元素。具体地说,对于在文本节点前面或后面带有连字符的链接元素,可以使用单独的链接元素

试试这个XSLT。这就利用intersect函数来获取003-006所需的元素范围

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="kFloat" match="figure" use="@xml_id"/>

<xsl:template match="node()|@*" name="identity">
    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>

<xsl:template match="p/link[following-sibling::node()[1][self::text()][. = '-']][following-sibling::node()[2][self::link]]" priority="3">
    <xsl:call-template name="identity" />
    <xsl:apply-templates select="key('kFloat', substring-after(@href, '#'))" mode="float" />
</xsl:template>

<xsl:template match="p/link[preceding-sibling::node()[1][self::text()][. = '-']][preceding-sibling::node()[2][self::link]]" priority="2">
    <xsl:call-template name="identity" />
    <xsl:variable name="firstLink" select="preceding-sibling::node()[2]" />
    <xsl:apply-templates select="key('kFloat', substring-after($firstLink/@href, '#'))/following-sibling::figure intersect key('kFloat', substring-after(@href, '#'))/preceding-sibling::figure" mode="float" />
    <xsl:apply-templates select="key('kFloat', substring-after(@href, '#'))" mode="float" />
</xsl:template>

<xsl:template match="p/link[@href]">
    <xsl:next-match />
    <xsl:variable name="doc" select="/" />
    <xsl:for-each select="for $ref in tokenize(@href, ' ') return substring-after($ref, '#')">
        <xsl:apply-templates select="key('kFloat', ., $doc)" mode="float" />
    </xsl:for-each>
</xsl:template>

<xsl:template match="figure" mode="float">
    <float id="{@xml_id}"/>    
</xsl:template>
</xsl:stylesheet>

请参见

在Tim Sir的帮助下,我修改了我的答案如下

<xsl:value-of select="for $i in key('kFloat', $varRangeLast)/preceding-sibling::figure
[some $k in preceding-sibling::figure satisfies  
 contains($k/@xml_id, $varRangeFirst)] return $i/@xml_id"/>
XSLT2:


谢谢你的建议,特别是交叉点功能。还有一个…先生,根据你的建议,我得到了解决方案,谢谢你的宝贵建议。。。
<root>
<p id="p1">This <link href="#fig-0001 #fig-0002"/><float id="fig-0001"/><float id="fig-0002"/>, <link href="#fig-0003"/><float id="fig-0003"/>-<link href="#fig-0006"/><float id="fig-0004"/><float id="fig-0005"/><float id="fig-0006"/></p>
<figure xml_id="fig-0001"><label>Fig. 1</label><caption><p>One</p></caption></figure>
<figure xml_id="fig-0002"><label>Fig. 2</label><caption><p>Two</p></caption></figure>
<figure xml_id="fig-0003"><label>Fig. 3</label><caption><p>Three</p></caption></figure>
<figure xml_id="fig-0004"><label>Fig. 4</label><caption><p>Four</p></caption></figure>
<figure xml_id="fig-0005"><label>Fig. 5</label><caption><p>Five</p></caption></figure>
<figure xml_id="fig-0006"><label>Fig. 6</label><caption><p>Six</p></caption></figure>
</root>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="kFloat" match="figure" use="@xml_id"/>

<xsl:template match="node()|@*" name="identity">
    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>

<xsl:template match="p/link[following-sibling::node()[1][self::text()][. = '-']][following-sibling::node()[2][self::link]]" priority="3">
    <xsl:call-template name="identity" />
    <xsl:apply-templates select="key('kFloat', substring-after(@href, '#'))" mode="float" />
</xsl:template>

<xsl:template match="p/link[preceding-sibling::node()[1][self::text()][. = '-']][preceding-sibling::node()[2][self::link]]" priority="2">
    <xsl:call-template name="identity" />
    <xsl:variable name="firstLink" select="preceding-sibling::node()[2]" />
    <xsl:apply-templates select="key('kFloat', substring-after($firstLink/@href, '#'))/following-sibling::figure intersect key('kFloat', substring-after(@href, '#'))/preceding-sibling::figure" mode="float" />
    <xsl:apply-templates select="key('kFloat', substring-after(@href, '#'))" mode="float" />
</xsl:template>

<xsl:template match="p/link[@href]">
    <xsl:next-match />
    <xsl:variable name="doc" select="/" />
    <xsl:for-each select="for $ref in tokenize(@href, ' ') return substring-after($ref, '#')">
        <xsl:apply-templates select="key('kFloat', ., $doc)" mode="float" />
    </xsl:for-each>
</xsl:template>

<xsl:template match="figure" mode="float">
    <float id="{@xml_id}"/>    
</xsl:template>
</xsl:stylesheet>
<xsl:value-of select="for $i in key('kFloat', $varRangeLast)/preceding-sibling::figure
[some $k in preceding-sibling::figure satisfies  
 contains($k/@xml_id, $varRangeFirst)] return $i/@xml_id"/>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="node()|@*">
    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>

<xsl:key name="kFloat" match="*" use="@xml_id"/>
<xsl:template match="link[ancestor::p/following-sibling::*[1][matches(name(), '^(figure)$')]][matches(key('kFloat', if(contains(@href, ' ')) then substring-after(substring-before(@href, ' '), '#') else substring-after(@href, '#'))/name(), '^(figure)$')]">
    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy><!-- link element retaining-->
    <!--Range between  IDs  selection -->
    <xsl:if test="matches(preceding-sibling::node()[1][self::text()], '^(&amp;#x2013;|&amp;#x02013;|–|-)$')">
        <xsl:variable name="varRangeFirst" select="substring-after(preceding-sibling::node()[2][name()='link']/@href, '#')"/>
        <xsl:variable name="varRangeLast" select="substring-after(@href, '#')"/>
        <xsl:variable name="varRangeBetweenIDs1">
            <xsl:value-of select="for $i in key('kFloat', $varRangeLast)/preceding-sibling::figure[some $k in preceding-sibling::figure satisfies contains($k/@xml_id, $varRangeFirst)] return $i/@xml_id"/>
        </xsl:variable>

            <xsl:for-each select="tokenize($varRangeBetweenIDs1, ' ')">
                    <xsl:element name="float"><xsl:attribute name="id" select="."/></xsl:element>
            </xsl:for-each>
    </xsl:if>
    <xsl:for-each select="tokenize(@href, ' ')"><!--for each link's individual hrefs will have respective float element -->
        <float id="{substring-after(., '#')}"/>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>