处理位于两个节点之间的文本
Processing text that sits between two nodes
我有一个 XML,其中包含一些文本:
<p>Sentence blah blah blah <a_href=... />,<a_href=... />,<a_href=... />.</p>
a_href 标签将作为上标输出(使用 CSS 为 a_href 标签设置上标),我想要 [=21= 之间的逗号] 元素也可以获得上标。所以我正在寻找具有以下结果的转换:
<p>Sentence blah blah blah <a_href... /><sup>,</sup><a_href... /><sup>,</sup><a_href... />.</p>
我不认为我可以使用 Xpath 来 select 只有文本节点的一部分,所以没有办法找到 "an a_href tag followed by a comma and another a_href tag"。
我可以检查 a_href 标签后面是否跟着另一个 a_href 标签,但无法检查它们之间的内容?
只有在 a_href 节点之间有一个逗号,或一个逗号和一个 space 时,才会出现上标。如果有更多的文字,它不应该有上标。
(编辑:将标签重命名为 a_href 以消除歧义,在实际代码中没有下划线)
The superscript should happen only if there is a comma, or a comma and
one space between the a_href nodes.
给定一个格式正确的 (!) 输入,例如:
XML
<p>Start <a href="abc"/>,<a href="def"/>, middle <a href="ghi"/>, <a href="jkl"/> and end.</p>
以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[normalize-space(.)=',' and preceding-sibling::*[1][self::a] and following-sibling::*[1][self::a]]">
<sup>
<xsl:value-of select="."/>
</sup>
</xsl:template>
</xsl:stylesheet>
将return:
<?xml version="1.0" encoding="UTF-8"?>
<p>Start <a href="abc"/>
<sup>,</sup>
<a href="def"/>, middle <a href="ghi"/>
<sup>, </sup>
<a href="jkl"/> and end.</p>
注:
你说 a
标签被 CSS 设置为上标;我对 CSS 了解不多,但我怀疑它也能处理这个任务。
我有一个 XML,其中包含一些文本:
<p>Sentence blah blah blah <a_href=... />,<a_href=... />,<a_href=... />.</p>
a_href 标签将作为上标输出(使用 CSS 为 a_href 标签设置上标),我想要 [=21= 之间的逗号] 元素也可以获得上标。所以我正在寻找具有以下结果的转换:
<p>Sentence blah blah blah <a_href... /><sup>,</sup><a_href... /><sup>,</sup><a_href... />.</p>
我不认为我可以使用 Xpath 来 select 只有文本节点的一部分,所以没有办法找到 "an a_href tag followed by a comma and another a_href tag"。 我可以检查 a_href 标签后面是否跟着另一个 a_href 标签,但无法检查它们之间的内容? 只有在 a_href 节点之间有一个逗号,或一个逗号和一个 space 时,才会出现上标。如果有更多的文字,它不应该有上标。
(编辑:将标签重命名为 a_href 以消除歧义,在实际代码中没有下划线)
The superscript should happen only if there is a comma, or a comma and one space between the a_href nodes.
给定一个格式正确的 (!) 输入,例如:
XML
<p>Start <a href="abc"/>,<a href="def"/>, middle <a href="ghi"/>, <a href="jkl"/> and end.</p>
以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[normalize-space(.)=',' and preceding-sibling::*[1][self::a] and following-sibling::*[1][self::a]]">
<sup>
<xsl:value-of select="."/>
</sup>
</xsl:template>
</xsl:stylesheet>
将return:
<?xml version="1.0" encoding="UTF-8"?>
<p>Start <a href="abc"/>
<sup>,</sup>
<a href="def"/>, middle <a href="ghi"/>
<sup>, </sup>
<a href="jkl"/> and end.</p>
注:
你说 a
标签被 CSS 设置为上标;我对 CSS 了解不多,但我怀疑它也能处理这个任务。