【问题标题】:Convert an xml element whose content is inside CDATA转换内容在 CDATA 内的 xml 元素
【发布时间】:2011-01-05 05:51:39
【问题描述】:

我有一个如下所示的 xml 片段

<Detail uid="6">
    <![CDATA[
    <div class="heading">welcome to my page</div>
    <div class="paragraph">this is paraph</div>
    ]]>
</Detail>

我希望能够改变

<div class="heading">...</div> to <h1>Welcome to my page</h1>
<div class="paragraph">...</div> to <p>this is paragraph</p>

你知道我如何在 xslt 1.0 中做到这一点

【问题讨论】:

  • 根据定义,CDATA 部分不需要格式正确的内容,因此假设可以被解析为 XML 可能是不安全的
  • 在这种情况下,它的格式正确。 xml 的结构就是这样设计的。是否可以删除

标签: xml xslt cdata


【解决方案1】:

运行两个转换怎么样。

通过 1.)

<?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" indent="yes" encoding="UTF-8"/>

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

    <xsl:template match="Detail">
        <Detail>
            <xsl:copy-of select="@*"/>
        <xsl:value-of select="." disable-output-escaping="yes" />
        </Detail>
    </xsl:template>

</xsl:stylesheet>

将产生:

<?xml version="1.0" encoding="UTF-8"?>
<Detail uid="6"> 
    <div class="heading">welcome to my page</div>
    <div class="paragraph">this is paraph</div>
</Detail>

通过 2。)

<?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" indent="yes" encoding="UTF-8"/>

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

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

    <xsl:template match="div[@class='heading']">
        <h1><xsl:value-of select="."/></h1>
    </xsl:template>

    <xsl:template match="div[@class='paragraph']">
        <p><xsl:value-of select="."/></p>
    </xsl:template>

</xsl:stylesheet>

生产:

<?xml version="1.0" encoding="UTF-8"?>
<Detail uid="6">
<h1>welcome to my page</h1>
<p>this is paraph</p>
</Detail>

【讨论】:

【解决方案2】:

您不能告诉 XSL 1.0 从 CDATA 中提取字符串并将其解析为 XML。

【讨论】:

    【解决方案3】:

    您不能“删除” CDATA,但可以粗略地实现所需的输出:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
       <Detail>
            <xsl:variable name="before" select="substring-before(//Detail,'&lt;div class=&quot;heading&quot;&gt;')" />
            <xsl:variable name="afteropen" select="substring-after(//Detail,'&lt;div class=&quot;heading&quot;&gt;')" />
            <xsl:variable name="body" select="substring-before($afteropen, '&lt;/div&gt;')" />
            <xsl:variable name="after" select="substring-after($afteropen, '&lt;/div&gt;')" />
            <xsl:value-of select="concat($before, '&lt;h1&gt;', $body, '&lt;/h1&gt;',$after)"
                    disable-output-escaping="yes"       />
       </Detail>
    </xsl:template>
    </xsl:stylesheet>
    

    这适用于您尝试解析的第一种类型的 div,您可以遵循与第二种类似的方法。可以通过一些努力使其更通用。

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 2015-03-06
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多