how to remove specific elements but not the text inside it using xslt

牧云@^-^@ 提交于 2020-01-06 12:47:13

问题


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())]" />


来源:https://stackoverflow.com/questions/28362715/how-to-remove-specific-elements-but-not-the-text-inside-it-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!