将float-anchor标签与第一个相应的link标签一起放置

时间:2019-08-07 09:12:54

标签: xslt-2.0

请建议将元素与第一个元素一起放置,但是放置float-anchor标记的顺序应与文件中指定的顺序相同(图和表应立即放置在“ link”之后引用)。

请参见下面的输入xml,在

中,第一个不应该使用浮点锚,第二个应该获得浮动锚,因为浮动锚的顺序应为fig-0003A,fig-0003B,tab-0001A。

XML:

<root>
<p id="p1">This is <link href="#fig-0001"/> <link href="#fig-0002"/> <link href="#fig-0002"/></p>
<figure xml_id="fig-0001"><label>1</label><caption><p>One</p></caption></figure>
<figure xml_id="fig-0002"><label>2</label><caption><p>Two</p></caption></figure>

<p id="p2">This is <link href="#fig-0001"/> <link href="#fig-0002"/></p>
<p id="p3">This is empty content.</p>

<p id="p4">This is <link href="#fig-0003B"/> other text <link href="#fig-0003A"/> other is also <link href="#fig-0003B"/> <link href="#fig-0001"/> <link href="#tab-0001A"/> here</p>
<figure xml_id="fig-0003A"><label>3A</label><caption><p>ThreeA</p></caption></figure>
<figure xml_id="fig-0003B"><label>3B</label><caption><p>ThreeB</p></caption></figure>
<table xml_id="tab-0001A"><label>Table 1</label><caption>Table 1 caption</caption></table>

<p id="p5">This is <list><item>list1 <link href="#fig-0005"/></item></list> other text <link href="#fig-0004"/> other is also here <link href="#fig-0005"/> and <link href="#fig-0003"/></p>
<figure xml_id="fig-0004"><label>4</label><caption><p>4</p></caption></figure>
<figure xml_id="fig-0005"><label>5</label><caption><p>5</p></caption></figure>

</root>

XSLT 2:

<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:template match="link[ancestor::p/following-sibling::*[1]
 [matches(name(), '^(table|figure)$')]]"><!-- selecting link elements, which are having fig and tables next those cited paras immediately-->

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

    <xsl:variable name="varLinkID">
        <xsl:if test="not(@href=preceding::link
    [generate-id(ancestor::p)=generate-id(current()/ancestor::p)]/@href)">
      <xsl:value-of select="substring-after(@href, '#')"/></xsl:if>
        <xsl:value-of select="substring-after(@href, '#')"/>
    </xsl:variable><!--Identifying very first citation -->

    <xsl:for-each select="ancestor::p/following-sibling::*
      [matches(name(), '^(table|figure)$')]
      [generate-id(following-sibling::p[1])=generate-id(current()/ancestor::p/following-sibling::p[1])]">
        <xsl:variable name="varFloatID"><xsl:value-of select="@xml_id"/></xsl:variable>

        <xsl:if test="$varFloatID=$varLinkID">
         <xsl:element name="float-anchor">
           <xsl:attribute name="refID" select="$varFloatID"/>
         </xsl:element>
        </xsl:if>

    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

所需结果:

<?xml version="1.0" encoding="UTF-8"?><root>
<p id="p1">This is <link href="#fig-0001"/><float-anchor refID="fig-0001"/> <link href="#fig-0002"/><float-anchor refID="fig-0002"/> <link href="#fig-0002"/></p>
<figure xml_id="fig-0001"><label>1</label><caption><p>One</p></caption></figure>
<figure xml_id="fig-0002"><label>2</label><caption><p>Two</p></caption></figure>

<p id="p2">This is <link href="#fig-0001"/> <link href="#fig-0002"/></p>
<p id="p3">This is empty content.</p>

<p id="p4">This is <link href="#fig-0003B"/><!--ignore this comment, dont place the float-anchor here, because Fig 3A float-anchor should place first--> other text <link href="#fig-0003A"/><float-anchor refID="fig-0003A"/><!-- comment for explantion, ignore this, <figure xml_id="fig-0003A">, <figure xml_id="fig-0003B">, and <table xml_id="tab-0001A"> are placed below in this sequence --> other is also <link href="#fig-0003B"/><float-anchor refID="fig-0003B"/> <link href="#fig-0001"/> <link href="#tab-0001A"/><float-anchor refID="tab-0001A"/> here</p>
<figure xml_id="fig-0003A"><label>3A</label><caption><p>ThreeA</p></caption></figure>
<figure xml_id="fig-0003B"><label>3B</label><caption><p>ThreeB</p></caption></figure>
<table xml_id="tab-0001A"><label>Table 1</label><caption>Table 1 caption</caption></table>

<p id="p5">This is <list><item>list1 <link href="#fig-0005"/></item></list> other text <link href="#fig-0004"/><float-anchor refID="fig-0004"/> other is also here <link href="#fig-0005"/><float-anchor refID="fig-0005"/> and <link href="#fig-0003"/></p>
<figure xml_id="fig-0004"><label>4</label><caption><p>4</p></caption></figure>
<figure xml_id="fig-0005"><label>5</label><caption><p>5</p></caption></figure>

</root>

0 个答案:

没有答案