XSLT替换节点中的html

问题描述 投票:0回答:1

我不知道如何将guiLabel转换为强大的html输出我试图得到以下html

输出:

<p>lorem 1</p>
<p>lorem ipsum <strong>dolore</strong> amet</p>
<p>lorem 3</p>

从以下xml:

<para>1</para>
<para>lorem ipsum <guiLabel>dolore</guiLabel> amet</para>
<para>3</para>

我的xsl文件:

<xsl:for-each select="./*">
    <xsl:choose>
        <xsl:when test=". instance of element(para)">
            <p><xsl:value-of select="."/></p>
        </xsl:when>
    </xsl:choose>
</xsl:for-each>
xml xslt xslt-2.0
1个回答
1
投票

你应该使用模板化方法,使用xsl:for-each,然后使用单独的模板匹配你想要改变的元素,而不是做xsl:apply-templates

试试这个XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

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

  <xsl:template match="guiLabel">
    <strong>
      <xsl:apply-templates />
    </strong>
  </xsl:template>  
</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.