0

我有下面的 xml 文档。在这里,我想制作一个 xslt 文档以仅选择“。”之前的数字 并附加它来制作一个像'BVI-Chapter01'这样的字符串,并且只给这个字符串一个锚标记,这里01根据'.'之前的数字改变。在下面的 xml 文档中。

<index>
<title>INDEX</title>
<indexdiv>
    <title>A</title>
    <indexentry>
        <primaryie>
            <content-style font-style="bold">Administration</content-style>
        </primaryie>
        <secondaryie>administration order 7.251, 7.254</secondaryie>
        <tertiaryie level="1">application for 7.255-7.257</tertiaryie>
        <tertiaryie level="2">conditions to be satisfied 7.258-7.260</tertiaryie>
        <tertiaryie level="1">blocking</tertiaryie>
        <tertiaryie level="2">qualifying administrative receiver 7.261-7.263</tertiaryie>
        <tertiaryie level="1">discharge of 7.288-7.280</tertiaryie>
        <tertiaryie level="1">effect of 7.264</tertiaryie>
        <tertiaryie level="2">directors not terminated 7.265, 7.266</tertiaryie>
        <tertiaryie level="1">moratorium 7.267</tertiaryie>
        <tertiaryie level="2">administrative receiver, appointment 7.270</tertiaryie>
        <tertiaryie level="2">commencement 7.268</tertiaryie>
        <tertiaryie level="2">disposal of changed assets 7.269</tertiaryie>
        <tertiaryie level="2">judicial decisions 7.273</tertiaryie>
        <secondaryie>administrator 7.274, <content-style font-style="italic">see also </content-style>
            <content-style font-style="bold">Administrators</content-style>
        </secondaryie>
        <secondaryie>British Virgin Island companies 7.250</secondaryie>
        <secondaryie>creditors, role of 7.283</secondaryie>
        <tertiaryie level="1">application to court for directions 7.286</tertiaryie>
        </indexentry>
        </indexdiv>
        </index>

我希望输出如下。

<div class="index">
            <a name="BVI_IDX_01"> </a>
            <div class="index-title">INDEX</div>
            <div class="indexdiv">
                <div class="indexentry">
                    <div class="primaryie">
                        <span class="font-style-bold">Administration</span>
                    </div>
                    <div class="secondaryie">administration order <a href="er:#BVI_CH_07">7.251</a>, <a href="er:#BVI_CH_07">7.254</a>
                    </div>
                    <div class="tertiaryie-1">application for <a href="er:#BVI_CH_07">7.255</a>&#x2013;<a href="er:#BVI_CH_07">7.257</a>
                    </div>
                    <div class="tertiaryie-2">conditions to be satisfied <a href="er:#BVI_CH_07/P7-258">7.258</a>&#x2013;<a href="er:#BVI_CH_07/P7-260">7.260</a>
                    </div>
                    <div class="tertiaryie-1">blocking</div>
                    <div class="tertiaryie-2">qualifying administrative receiver <a href="er:#BVI_CH_07/P7-261">7.261</a>&#x2013;<a href="er:#BVI_CH_07/P7-263">7.263</a>
                    </div>
                    <div class="tertiaryie-1">discharge of <a href="er:#BVI_CH_07">7.288</a>&#x2013;<a href="er:#BVI_CH_07">7.280</a>
                    </div>
                    <div class="tertiaryie-1">effect of <a href="er:#BVI_CH_07">7.264</a>
                    </div>
                    <div class="tertiaryie-2">directors not terminated <a href="er:#BVI_CH_07">7.265</a>, <a href="er:#BVI_CH_07">7.266</a>
                    </div>
                    <div class="tertiaryie-1">moratorium <a href="er:#BVI_CH_07">7.267</a>
                    </div>
                    <div class="tertiaryie-2">administrative receiver, appointment <a href="er:#BVI_CH_07">7.270</a>
                    </div>
                    <div class="tertiaryie-2">commencement <a href="er:#BVI_CH_07>7.268</a>
                    </div>
                    <div class="tertiaryie-2">disposal of changed assets <a href="er:#BVI_CH_07">7.269</a>
                    </div>
                    <div class="tertiaryie-2">judicial decisions <a href="er:#BVI_CH_07">7.273</a>
                    </div>
                    <div class="secondaryie">administrator <a href="er:#BVI_CH_07">7.274</a>, <span class="font-style-italic">see also</span>
                        <span class="font-style-bold">Administrators</span>
                    </div>
                    <div class="secondaryie">British Virgin Island companies <a href="er:#BVI_CH_07">7.250</a>
                    </div>
                    <div class="secondaryie">creditors, role of <a href="er:#BVI_CH_07">7.283</a>
                    </div>
                    <div class="tertiaryie-1">application to court for directions <a href="er:#BVI_CH_07">7.286</a>
                    </div>
                    <div class="tertiaryie-1">proposals, amendment <a href="er:#BVI_CH_07">7.284</a>, <a href="er:#BVI_CH_07">7.285</a>
                    </div>
                    <div class="tertiaryie-1">unfair prejudice <a href="er:#BVI_CH_07">7.287</a>, <a href="er:#BVI_CH_07">7.289</a>

谢谢

4

1 回答 1

0

XSLT 2.0 中的以下解决方案使用analyze-string 在文本节点中搜索所需的模式。

但是,这种转换并不能完全按照您的要求执行,因为我找不到这种链接的逻辑

<a href="er:#BVI_CH_07/P7-261">7.261</a>

打印而不是这些类型的链接

<a href="er:#BVI_CH_07">7.261</a>

另外我假设引用只能出现在二级和三级元素中。


XSLT 2.0

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

    <xsl:output method="html" indent="yes" />

    <!-- Use this string to replace all numbers on the text by a
         a different character which cannot be present in the text (#)-->
    <xsl:variable name="str-numbers" select="'0123456789'" />
    <xsl:variable name="str-special-char" select="'#'" />
    <xsl:variable name="str-special-seq" select="'##########'" />

    <!-- Build the index header structure -->
    <xsl:template match="index">
        <div class="index">
            <a name="BVI_IDX_01" />
            <div class="index-title"><xsl:value-of select="title" /></div>
            <div class="indexdiv">
                <xsl:apply-templates select="indexdiv" />
            </div>
        </div>
    </xsl:template>

    <!-- Ignore title from index div element -->
    <xsl:template match="indexdiv/title" />

    <!-- Transform main elements to divs -->
    <xsl:template match="indexentry|primaryie|secondaryie|indexdiv|indexentry">
        <div class="{local-name()}">
            <xsl:apply-templates select="node()" />
        </div>
    </xsl:template>

    <xsl:template match="tertiaryie">
        <div class="{concat(local-name(), '-', @level)}">
            <xsl:apply-templates select="node()" />
        </div>
    </xsl:template>

    <xsl:template match="content-style">
        <span>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(local-name(@*[1]), '-', @*[1])" />
                <xsl:for-each select="@*[position() > 1]">
                    <xsl:value-of select="concat(' ', local-name(), '-', .)" />
                </xsl:for-each>
            </xsl:attribute>
            <xsl:apply-templates select="node()" />
        </span>
    </xsl:template>

    <!-- Transform numbers from textnodes within tertiaryie
         and secondaryie elements -->
    <xsl:template match="secondaryie/text()|tertiaryie/text()">
        <!-- Use a regular expression to match the desired pattern
             within the text -->
        <xsl:analyze-string select="." regex="((\d+)\.\d+)">
            <!-- A reference is found, print hyperlink -->
            <xsl:matching-substring>
                <a href="{concat('er:#BVI_CH_', format-number(number(regex-group(2)), '00'))}">
                    <xsl:value-of select="regex-group(1)" />
                </a>
            </xsl:matching-substring>
            <!-- Output the text which do not match the given pattern -->
            <xsl:non-matching-substring>
                <xsl:value-of select="." />
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>


</xsl:stylesheet>
于 2013-02-25T14:48:24.783 回答