gpt4 book ai didi

xslt - XHTML。将
文本括在段落中,并使用 XSLT 1.0 将
转换为段落

转载 作者:行者123 更新时间:2023-12-04 19:17:57 25 4
gpt4 key购买 nike

我正在寻找一种快速简便的方法来使用 XSLT 1.0 转换他的 XML(类似于 XHTML):

<?xml version="1.0" encoding="UTF-8"?>
<html>
<head/>
<body>
<div>Hello<a href="http://google.com">this is the first</a>line.<p>This the second.<br/>And this the third one.</p></div>
</body>
</html>

对这个:

<?xml version="1.0" encoding="UTF-8"?>
<html>
<head/>
<body>
<div>
<p>Hello<a href="http://google.com">this is the first</a>line.</p>
<p>This the second.</p>
<p>And this the third one.</p>
</div>
</body>
</html>

我在考虑 XSLT 1.0 中的树遍历算法。什么是复杂的,例如所附 <a>链接。并且还存在 <p>不应该被删除。

有人可以帮我吗?非常感谢。

最佳答案

本次改造 :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*"/>

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

<xsl:template match="div[text() and p]">
<div>
<p>
<xsl:apply-templates select="node()[not(self::p or preceding-sibling::p)]"/>
</p>
<xsl:apply-templates select="p | p/following-sibling::node()"/>
</div>
</xsl:template>

<xsl:template match="p[text() and br]">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match=
"p/text()
[preceding-sibling::node()[1][self::br]
or
following-sibling::node()[1][self::br]
]">
<p><xsl:value-of select="."/></p>
</xsl:template>

<xsl:template match="p/br"/>
</xsl:stylesheet>

当应用于提供的 XML 文档时 :
<html>
<head/>
<body>
<div>Hello
<a href="http://google.com">this is the first</a>line.
<p>This the second.<br/>And this the third one.</p>
</div>
</body>
</html>

产生想要的、正确的结果 :
<html>
<head/>
<body>
<div>
<p>Hello
<a href="http://google.com">this is the first</a>line.
</p>
<p>This the second.</p>
<p>And this the third one.</p>
</div>
</body>
</html>

关于xslt - XHTML。将 <div> 文本括在段落中,并使用 XSLT 1.0 将 <br/> 转换为段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6995317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com