如何使用 xslt 删除 CDATA 中的元素

问题描述 投票:0回答:1
xml xslt cdata
1个回答
0
投票

正如您在评论中被告知的那样,对于纯 XSLT,这主要在 XSLT 3 中使用

parse-xml
函数和
serialize
函数是可行的:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:sch="http://purl.oclc.org/dsdl/schematron"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>
  
  <xsl:output cdata-section-elements="rule"/>

  <xsl:template match="rule">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:variable name="transformed">
        <xsl:apply-templates select="parse-xml(.)"/>
      </xsl:variable>
      <xsl:value-of select="serialize($transformed)"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="sch:rule[ends-with(@context, 'GenericFailureAnnouncementName')]"/>
  
</xsl:stylesheet>

使用早期版本的 XSLT,您需要检查处理器是否公开扩展函数或允许您调用底层平台以实现

parse-xml
serialize
功能。

© www.soinside.com 2019 - 2024. All rights reserved.