XPath:连接节点内的文本及其直接跟随的兄弟节点
XPath: Concatenate text within node and its directly following sibling
我是 XSLT 和 XPath 的初学者,主要通过示例学习并从事项目。
我正在尝试连接两个节点的 text()
内容。对于节点 a
的每个外观,节点 a
的 text()
内容应该与下一个兄弟节点 b
的 text()
内容连接起来。此串联内容应在与节点 a
相同的位置注册为 text()
新节点 foo
。
示例输入:
<html>
<body>
<a>First text</a>
<b>Text</b>
<c>Indiferent tag.</c>
<a>Another "a" test.</a>
<b>ěščřžýáíéúů</b>
<c>Another indiferent tag.</c>
</body>
</html>
预期(希望)输出:
<html>
<body>
<foo>First text Text</foo>
<c>Indiferent tag.</c>
<foo>Another "a" test. ěščřžýáíéúů</foo>
<c>Another indiferent tag.</c>
</body>
</html>
我目前使用的样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="html/body/a">
<foo>
<xsl:value-of select="concat(text(), folowing-sibling::b[1]/text())"/>
</foo>
</xsl:template>
只是因为错误的 XPath 语句而出错。
如何改进样式表以获得所需的结果?
注意: 我知道存在以下问题及其答案:Here, This brilliant answer 和其他问题,但我无法将它们应用到我的具体案例中。
此外,对于这个项目,我坚持python lxml
;我相信我只能使用 XSLT 1.0 和 XPath 1.0。这些是我希望任何好心人帮助我遵守的限制。但是,我正在学习 saxon
,因此从学习的角度来看,使用高级版本的解决方案也很好。
最后,这只是来自较大的 HTML 文件的一个示例片段,我正在尝试将其转换为有效的(并且对于我的用例来说很方便)XML,并且有很多涉及分组(我最终也需要按键 foo
进行分组),因此该解决方案最好使用身份转换重载,这有望使我能够在这个示例之外使用它 ...
这里有一个方法可以做到这一点。
虽然不确定问题末尾的分组评论...
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="a">
<foo>
<xsl:value-of select="concat(., following-sibling::b[1])"/>
</foo>
</xsl:template>
<!-- Remove b nodes -->
<xsl:template match="b"/>
</xsl:stylesheet>
我是 XSLT 和 XPath 的初学者,主要通过示例学习并从事项目。
我正在尝试连接两个节点的 text()
内容。对于节点 a
的每个外观,节点 a
的 text()
内容应该与下一个兄弟节点 b
的 text()
内容连接起来。此串联内容应在与节点 a
相同的位置注册为 text()
新节点 foo
。
示例输入:
<html>
<body>
<a>First text</a>
<b>Text</b>
<c>Indiferent tag.</c>
<a>Another "a" test.</a>
<b>ěščřžýáíéúů</b>
<c>Another indiferent tag.</c>
</body>
</html>
预期(希望)输出:
<html>
<body>
<foo>First text Text</foo>
<c>Indiferent tag.</c>
<foo>Another "a" test. ěščřžýáíéúů</foo>
<c>Another indiferent tag.</c>
</body>
</html>
我目前使用的样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="html/body/a">
<foo>
<xsl:value-of select="concat(text(), folowing-sibling::b[1]/text())"/>
</foo>
</xsl:template>
只是因为错误的 XPath 语句而出错。
如何改进样式表以获得所需的结果?
注意: 我知道存在以下问题及其答案:Here, This brilliant answer 和其他问题,但我无法将它们应用到我的具体案例中。
此外,对于这个项目,我坚持python lxml
;我相信我只能使用 XSLT 1.0 和 XPath 1.0。这些是我希望任何好心人帮助我遵守的限制。但是,我正在学习 saxon
,因此从学习的角度来看,使用高级版本的解决方案也很好。
最后,这只是来自较大的 HTML 文件的一个示例片段,我正在尝试将其转换为有效的(并且对于我的用例来说很方便)XML,并且有很多涉及分组(我最终也需要按键 foo
进行分组),因此该解决方案最好使用身份转换重载,这有望使我能够在这个示例之外使用它 ...
这里有一个方法可以做到这一点。
虽然不确定问题末尾的分组评论...
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="a">
<foo>
<xsl:value-of select="concat(., following-sibling::b[1])"/>
</foo>
</xsl:template>
<!-- Remove b nodes -->
<xsl:template match="b"/>
</xsl:stylesheet>