XSLT 将节点文本包裹在子节点值周围

标签 xslt word-wrap

所以我有一个 xml 文件,其中包含

...
<chapter>
    <para>This line has a quote <quote id="one"/>. Here is some more text.</para>
    <para>This also has a quote <quote id="two"/>. Here is some more text.</para>
</chapter>
<references>
    <source id="one">
        <author>Author 1</author>
        <title>Title 1</title>
        <year>2001</year>
    </source>
     <source id="two">
        <author>Author 2</author>
        <title>Title 2</title>
        <year>2002</year>
    </source>
</references>
...

我想输出一个xhtml

...
<p>This line has a quote <a href="#one>[1]</a>. Here is some more text.</p>
<p>This also has a quote <a href="#two>[2]</a>. Here is some more text.</p>
<h3>References</h3>
<ol>
    <li><a name="one">Author 1, Title 1, 2001</a></li>
    <li><a name="two">Author 2, Title 2, 2002</a></li>
</ol>
...

所以我想要的是文本内的引用,并带有指向引用列表中项目的链接。 我还希望引用文献按文本中出现的顺序排列。

最佳答案

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />

    <xsl:template match="para">
        <p><xsl:apply-templates/></p>
    </xsl:template>

    <xsl:template match="quote">
        <a href="#{@id}">
        <xsl:text>[</xsl:text>
        <xsl:number count="quote" level="any" />
        <xsl:text>]</xsl:text>
        </a>
    </xsl:template>

    <xsl:template match="references">
        <h3>References</h3>
        <ol>
            <xsl:apply-templates/>
        </ol>
    </xsl:template>

    <xsl:template match="source">
        <li>
            <a name="{@id}">
                <xsl:apply-templates/>
            </a>
        </li>
    </xsl:template>

    <xsl:template match="author|title">
        <xsl:value-of select="."/>
        <xsl:text>, </xsl:text>
    </xsl:template>

    <xsl:template match="year">
        <xsl:value-of select="."/>
    </xsl:template>

</xsl:stylesheet>

关于XSLT 将节点文本包裹在子节点值周围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688524/

相关文章:

xml - 使用 xsltproc 输出 XHMTL

java - 如何在保持安全性的同时提供 XSLT 实用程序

javascript - 使用 xsl 的 XML 日期格式

crystal-reports - 在Crystal报表中启用交叉表的增长

Flutter:如何将数学方程包装在容器中?

xml - XSLT 无法正常工作

xslt - xsl分组排序问题

Python textwrap 库 - 如何保留换行符?

css - 移动设备上的 Bootstrap Wordwrap 问题

html - 选择 "Top and Bottom"时,如何像 MS Word 那样在 HTML/CSS 中将文本环绕图像