how to remove specific elements but not the text i

2019-09-02 10:50发布

问题:

This is my Input xml

<para>
<a><b>this is a text</b></a>
</para>

this is my expected output

<para>
this is a text
</para>

how can i delete all the "a" tags and the "b" tags only and the text will not be affected using xslt thanks

回答1:

<xsl:template match="//para">
   <xsl:copy>
      <xsl:value-of select="."></xsl:value-of>
   </xsl:copy>
</xsl:template>

(or to avoid white space from other child elements:

<xsl:template match="//para">
   <xsl:copy>
      <xsl:value-of select="./*/*/text()"></xsl:value-of>
   </xsl:copy>
</xsl:template>


回答2:

Start with the identity transformation template

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

then add

<xsl:template match="a | b">
  <xsl:apply-templates/>
</xsl:template>

to handle your elements.



回答3:

Problem Solve..

<xsl:strip-space elements="*"/>
 <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

<xsl:template match="//*/text()">
  <xsl:if test="normalize-space(.)">
    <xsl:value-of select=
     "concat(normalize-space(.), '&#xA;')"/>
  </xsl:if>
  <xsl:apply-templates />
</xsl:template>
<xsl:template match="*[not(node())]" />