使用 XSLT 转换 XML 内的 HTML 标签

transform HTML tags within XML using XSLT

我有以下源 XML 文件:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<employees>
<employee>
<html-content>
<p>This is the first text.<br/>
With multiple lines.<br/>
And maybe one more.</p>
<p>This is the second text.<br/>
With multiple lines.<br/>
And maybe one more.</p>
</html-content>
</employee>
</employees>

我想用XSLT把它改成这样

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<employees>
<employee>
<info>
<text>
<content>This is the first text.</content>
<br/>
<content>With multiple lines.</content>
<br/>
<content>And maybe one more.</content>
</text>
<text>
<content>This is the second text.</content>
<br/>
<content>With multiple lines.</content>
<br/>
<content>And maybe one more.</content>
</text>
</info>
</employee>
</employees>

总结

到目前为止我已经有了这个 XSLT,但是它只是把所有 <br/> 放在最后,所以这对我没有真正的帮助。

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"   
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match = "/">
<employees>
<xsl:for-each select="employees/employee">
<employee>
<info>
<xsl:apply-templates select="html-content"/>
</info>
</employee>
</xsl:for-each>
</employees>
</xsl:template>

<xsl:template match="p">
<text>
<xsl:value-of select="."/>
<xsl:apply-templates select="br"/>
</text>
</xsl:template>

<xsl:template match="br">
<br-found/>
</xsl:template>

</xsl:stylesheet>

这导致:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
   <employee>
      <info>
         <text>This is the first text.
With multiple lines.
And maybe one more.<br-found/>
            <br-found/>
         </text>
         <text>This is the second text.
With multiple lines.
And maybe one more.<br-found/>
            <br-found/>
         </text>
      </info>
   </employee>
</employees>

有人可以指点一下吗?

也许你可以简单地做:

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="html-content">
    <info>
        <xsl:apply-templates/>
    </info>
</xsl:template>

<xsl:template match="p">
    <text>
        <xsl:for-each select="text()">
            <content>
                <xsl:value-of select="."/>
            </content>
        </xsl:for-each>
    </text>
</xsl:template>

</xsl:stylesheet>

使用您的代码,您可以将 p 的模板更新为:

<xsl:template match="p">
  <text>
    <xsl:apply-templates/>
  </text>
</xsl:template>

并添加一个额外的模板如下:

<xsl:template match="p/text()">
    <content>
        <xsl:value-of select="."/>
    </content>
</xsl:template>