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 将属性值更改为具有相应属性值的另一个元素的位置_Xslt_Xslt 2.0 - Fatal编程技术网

Xslt 将属性值更改为具有相应属性值的另一个元素的位置

Xslt 将属性值更改为具有相应属性值的另一个元素的位置,xslt,xslt-2.0,Xslt,Xslt 2.0,我有一个XHTML文档,其中包含span和div元素,这些元素使用id和epub:type属性引用打印版本的分页符。例如:。该文档还具有指向这些元素的链接,例如: 此单个XHTML文档将被拆分为多个XHTML文档以形成一个EPUB包。因此,需要更新href属性以匹配相应id的新位置。例如:。新XHTML文件的名称等于主体/部分元素的位置。因此,在上一个示例中,带有id=“page-3”的分页符显然位于第二个body/section元素中 我正在使用以下XSLT 2.0样式表: <!--id

我有一个XHTML文档,其中包含
span
div
元素,这些元素使用
id
epub:type
属性引用打印版本的分页符。例如:
。该文档还具有指向这些元素的链接,例如:

此单个XHTML文档将被拆分为多个XHTML文档以形成一个EPUB包。因此,需要更新
href
属性以匹配相应
id
的新位置。例如:
。新XHTML文件的名称等于
主体/部分
元素的位置。因此,在上一个示例中,带有
id=“page-3”
的分页符显然位于第二个
body/section
元素中

我正在使用以下XSLT 2.0样式表:

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

<!--variable to match id of elements with pagebreak values-->
<xsl:variable name="page-id" select="//*[@epub:type = 'pagebreak']/@id"/>

<!--update href attributes to match new filenames-->
<xsl:template match="a/@href">
    <xsl:choose>
        <xsl:when test="tokenize(., '#')[last()] = $page-id">
            <xsl:attribute name="href">
                <xsl:number count="//body/section[$page-id = tokenize(., '#')[last()]]" format="01"/>
                <xsl:value-of select="concat('.xhtml', .)"/>
            </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="."/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
这是我得到的输出:

<body>
    <section>
        <p>Link to page 3: <a href=".xhtml#page-3">3</a></p>
    </section>
    <section>      
        <div epub:type="pagebreak" id="page-3"/>
    </section>
</body>

链接至第3页:

这是我想要的输出:

<body>
    <section>
        <p>Link to page 3: <a href="02.xhtml#page-3">3</a></p>
    </section>
    <section>      
        <div epub:type="pagebreak" id="page-3"/>
    </section>
</body>

链接至第3页:

似乎
xsl:number
中的XPath表达式没有返回结果,但我不知道为什么。有谁能帮我做这个吗?

我想你需要

<xsl:template match="body/section" mode="number">
  <xsl:number format="01"/>
<xsl:template>

而不是

 <xsl:number count="//body/section[$page-id = tokenize(., '#')[last()]]" format="01"/>

使用


加上一个关键声明

<xsl:key name="page-id" match="body/section" use=".//*[@epub:type = 'pagebreak']/@id"/>

这似乎有效,谢谢!我以前没有使用过mode属性,所以我将仔细阅读。
 <xsl:apply-templates select="key('page-id', substring-after(., '#'))" mode="number"/>
<xsl:key name="page-id" match="body/section" use=".//*[@epub:type = 'pagebreak']/@id"/>