转换内容在 CDATA 内的 xml 元素 [英] Convert an xml element whose content is inside CDATA

查看:26
本文介绍了转换内容在 CDATA 内的 xml 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的 xml 片段

<![CDATA[<div class="heading">欢迎来到我的页面</div><div class="paragraph">这是段落</div>]]></详细信息>

我希望能够改变

...

到<h1>欢迎来到我的页面</h1><div class="paragraph">...</div><p>这是段落</p>

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

解决方案

运行两个转换怎么样.

通过 1.)

<xsl:output method="xml" indent="yes" encoding="UTF-8"/><xsl:template match="/"><xsl:apply-templates/></xsl:模板><xsl:template match="详细信息"><详情><xsl:copy-of select="@*"/><xsl:value-of select="."disable-output-escaping="yes"/></详细信息></xsl:模板></xsl:stylesheet>

将产生:

通过 2.)

<xsl:output method="xml" indent="yes" encoding="UTF-8"/><xsl:template match="/"><xsl:apply-templates/></xsl:模板><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*| node()"/></xsl:copy></xsl:模板><xsl:template match="div[@class='heading']"><h1><xsl:value-of select="."/></h1></xsl:模板><xsl:template match="div[@class='paragraph']"><p><xsl:value-of select="."/></p></xsl:模板></xsl:stylesheet>

产生:

I have a xml fragment like below

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

and I want to be able to change the

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

do you know how I can do that in xslt 1.0

解决方案

What about running two transforms.

Pass 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>

Will produce:

<?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>

Pass 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>

Produces:

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

这篇关于转换内容在 CDATA 内的 xml 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆