输入是 -
<data>
<container>
<strong>Code Development</strong>
<br/>
here to support
<ul class="nested">
<li>list 1</li>
<li>list 2</li>
<li>list 3</li>
<li>list 4</li>
</ul>
something here
<strong>this is strong</strong>
</container>
</data>
预期产出 -
<h3>Code Development</h3>
<p>
here to support
<ul class="nested">
<li>list 1</li>
<li>list 2</li>
<li>list 3</li>
<li>list 4</li>
</ul>
</p>
<h3>this is strong</h3>
<p> something here</p>
强标记应该变成h3,因为h3不能在<p>
内,所以将它移到p之外作为第一个出现在p之前。
答案 0 :(得分:0)
试试这个:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="strong">
<h3><xsl:value-of select="."/></h3>
</xsl:template>
<xsl:template match="container/text()">
<xsl:if test="string-length(normalize-space()) > 0">
<p><xsl:value-of select="."/></p>
</xsl:if>
</xsl:template>
<xsl:template match="ul">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>