我正在寻找一种快速简便的方法来使用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>
Run Code Online (Sandbox Code Playgroud)
到这一个:
<?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>
Run Code Online (Sandbox Code Playgroud)
我在想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>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的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>
Run Code Online (Sandbox Code Playgroud)
产生想要的,正确的结果:
<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>
Run Code Online (Sandbox Code Playgroud)