Xslt 如何根据其他元素的文本节点中的值更改HTML文档的元素序列
我有以下Html文档。Xslt 如何根据其他元素的文本节点中的值更改HTML文档的元素序列,xslt,xpath,xslt-2.0,Xslt,Xpath,Xslt 2.0,我有以下Html文档。 <html> <head><title>...</title></head> <body> <div class="figure-wrapper" id="figure1">...</div> <p class="para">Lorem Ipsum (see Fig. 1). Lorem Ipsum (see Fi
<html>
<head><title>...</title></head>
<body>
<div class="figure-wrapper" id="figure1">...</div>
<p class="para">Lorem Ipsum (see Fig. 1). Lorem Ipsum (see Fig. 2).</p>
<div class="figure-wrapper" id="figure3">...</div>
<p class="para">Lorem Ipsum (see Fig. 3). Lorem Ipsum (see Fig. 1).</p>
<div class="figure-wrapper" id="figure2">...</div>
</body>
</html>
...
...
Lorem Ipsum(见图1)。Lorem Ipsum(见图2)
...
Lorem Ipsum(见图3)。Lorem Ipsum(见图1)
...
我想要实现什么
- 将每个图形元素(由
元素包裹的元素)放在第一个引用它的段落之后李> - 如果第一段后面的元素本身是一个figure元素,那么应该将讨论中的figure元素放在后面
这里有一种不同的方法,您可以探索。我是在XSLT1.0中这样做的,但这些差异对于该方法来说并不是必不可少的
基本思想是将父段落的id附加到段落包含的每个引用。然后,使用Muenchian分组,我们只保留每个引用的第一次出现。由于每一个都保留了原始父对象的id,我们知道它需要在最终输出中出现的位置
注意,假设没有独立的参考元素(即至少一个段落中未引用的元素)
图形
应用于您的输入,将获得以下结果:
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<title>...</title>
</head>
<body>
<p class="para">Lorem Ipsum (see Fig. 1). Lorem Ipsum (see Fig. 2).</p>
<div class="figure-wrapper" id="figure1">...</div>
<div class="figure-wrapper" id="figure2">...</div>
<p class="para">Lorem Ipsum (see Fig. 3). Lorem Ipsum (see Fig. 1).</p>
<div class="figure-wrapper" id="figure3">...</div>
</body>
</html>
...
Lorem Ipsum(见图1)。Lorem Ipsum(见图2)
...
...
Lorem Ipsum(见图3)。Lorem Ipsum(见图1)
...
以下是我对XSLT2.0的建议,XSLT2.0在第一步中使用分析字符串
将例如(见图3)
转换为元素
,然后在第二步中使用键标识p
元素中的第一个引用以输出div[@class='figure wrapper']
。第二步还将ref
元素转换回内联文本:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:variable name="references">
<xsl:apply-templates mode="references"/>
</xsl:variable>
<xsl:template match="@* | node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@* , node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<!-- might want to use match="p[@class = 'para']//text()" -->
<xsl:template match="text()" mode="references" priority="5">
<xsl:analyze-string select="." regex="\(see Fig\. ([0-9]+)\)">
<xsl:matching-substring>
<ref name="figure" idref="{regex-group(1)}"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:key name="refs" match="div[@class = 'figure-wrapper']" use="@id"/>
<xsl:key name="fig-refs" match="ref" use="concat(@name, @idref)"/>
<xsl:template match="/">
<xsl:apply-templates select="$references/node()"/>
</xsl:template>
<xsl:template match="div[@class = 'figure-wrapper']"/>
<xsl:template match="p[@class = 'para'][.//ref[. is key('fig-refs', concat(@name, @idref))[1]]]">
<xsl:next-match/>
<xsl:variable name="first-refs" select=".//ref[. is key('fig-refs', concat(@name, @idref))[1]]"/>
<xsl:copy-of select="key('refs', $first-refs/concat(@name, @idref))"/>
</xsl:template>
<xsl:template match="ref">
<xsl:text>(see Fig. </xsl:text>
<xsl:value-of select="@idref"/>
<xsl:text>)</xsl:text>
</xsl:template>
</xsl:stylesheet>
(见图。
)
将带有Saxon 9.5的XSLT应用于您的输入
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>...</title>
</head>
<body>
<p class="para">Lorem Ipsum (see Fig. 1). Lorem Ipsum (see Fig. 2).</p>
<div class="figure-wrapper" id="figure1">...</div>
<div class="figure-wrapper" id="figure2">...</div>
<p class="para">Lorem Ipsum (see Fig. 3). Lorem Ipsum (see Fig. 1).</p>
<div class="figure-wrapper" id="figure3">...</div>
</body>
</html>
...
Lorem Ipsum(见图1)。Lorem Ipsum(见图2)
...
...
Lorem Ipsum(见图3)。Lorem Ipsum(见图1)
...
我想这是你想要的元素的顺序
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="tokens" match="token" use="." />
<xsl:key name="ref" match="div[@class='figure-wrapper']" use="@id" />
<xsl:variable name="root" select="/"/>
<!-- 1. collect all references, along with their parent id -->
<xsl:variable name="references">
<xsl:for-each select="//p[@class='para']">
<xsl:call-template name="cat_ref">
<xsl:with-param name="string" select="."/>
<xsl:with-param name="pid" select="generate-id()"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<!-- 2. keep only unique references -->
<xsl:variable name="unique-ref" select="exsl:node-set($references)/token[count(. | key('tokens', .)[1]) = 1]"/>
<!-- 3. output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p[@class='para']">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- append my references -->
<xsl:for-each select="$unique-ref[@pid=generate-id(current())]">
<xsl:variable name="ref-key" select="."/>
<!-- switch back to document in order to use key -->
<xsl:for-each select="$root">
<xsl:copy-of select="key('ref', $ref-key)"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<!-- suppress references -->
<xsl:template match="div [@class='figure-wrapper']"/>
<!-- proc template -->
<xsl:template name="cat_ref">
<xsl:param name="string"/>
<xsl:param name="pid"/>
<xsl:param name="prefix" select="'(see Fig. '" />
<xsl:param name="suffix" select="')'" />
<xsl:if test="contains($string, $prefix) and contains(substring-after($string, $prefix), $suffix)">
<token pid="{$pid}">
<xsl:text>figure</xsl:text>
<xsl:value-of select="substring-before(substring-after($string, $prefix), $suffix)" />
</token>
<!-- recursive call -->
<xsl:call-template name="cat_ref">
<xsl:with-param name="string" select="substring-after(substring-after($string, $prefix), $suffix)" />
<xsl:with-param name="pid" select="$pid" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<title>...</title>
</head>
<body>
<p class="para">Lorem Ipsum (see Fig. 1). Lorem Ipsum (see Fig. 2).</p>
<div class="figure-wrapper" id="figure1">...</div>
<div class="figure-wrapper" id="figure2">...</div>
<p class="para">Lorem Ipsum (see Fig. 3). Lorem Ipsum (see Fig. 1).</p>
<div class="figure-wrapper" id="figure3">...</div>
</body>
</html>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:variable name="references">
<xsl:apply-templates mode="references"/>
</xsl:variable>
<xsl:template match="@* | node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@* , node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<!-- might want to use match="p[@class = 'para']//text()" -->
<xsl:template match="text()" mode="references" priority="5">
<xsl:analyze-string select="." regex="\(see Fig\. ([0-9]+)\)">
<xsl:matching-substring>
<ref name="figure" idref="{regex-group(1)}"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:key name="refs" match="div[@class = 'figure-wrapper']" use="@id"/>
<xsl:key name="fig-refs" match="ref" use="concat(@name, @idref)"/>
<xsl:template match="/">
<xsl:apply-templates select="$references/node()"/>
</xsl:template>
<xsl:template match="div[@class = 'figure-wrapper']"/>
<xsl:template match="p[@class = 'para'][.//ref[. is key('fig-refs', concat(@name, @idref))[1]]]">
<xsl:next-match/>
<xsl:variable name="first-refs" select=".//ref[. is key('fig-refs', concat(@name, @idref))[1]]"/>
<xsl:copy-of select="key('refs', $first-refs/concat(@name, @idref))"/>
</xsl:template>
<xsl:template match="ref">
<xsl:text>(see Fig. </xsl:text>
<xsl:value-of select="@idref"/>
<xsl:text>)</xsl:text>
</xsl:template>
</xsl:stylesheet>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>...</title>
</head>
<body>
<p class="para">Lorem Ipsum (see Fig. 1). Lorem Ipsum (see Fig. 2).</p>
<div class="figure-wrapper" id="figure1">...</div>
<div class="figure-wrapper" id="figure2">...</div>
<p class="para">Lorem Ipsum (see Fig. 3). Lorem Ipsum (see Fig. 1).</p>
<div class="figure-wrapper" id="figure3">...</div>
</body>
</html>