Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转换内容在CDATA中的xml元素_Xml_Xslt_Cdata - Fatal编程技术网

转换内容在CDATA中的xml元素

转换内容在CDATA中的xml元素,xml,xslt,cdata,Xml,Xslt,Cdata,我有一个如下所示的xml片段 <Detail uid="6"> <![CDATA[ <div class="heading">welcome to my page</div> <div class="paragraph">this is paraph</div> ]]> </Detail> 欢迎来到我的页面 这是帕拉 ]]> 我希望能够改变现状 <div class="

我有一个如下所示的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中做到这一点吗?您不能告诉XSL 1.0从CDATA中提取字符串并将其解析为XML。

您不能“删除”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,您可以遵循与第二种类似的操作。通过一些努力,它可以变得更通用。

运行两个转换怎么样

通过1。)


将产生:

<?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"?>
<Detail uid="6">
<h1>welcome to my page</h1>
<p>this is paraph</p>
</Detail>

欢迎来到我的页面
这是帕拉


根据定义,CDATA节不需要格式良好的内容,因此假设可以解析为XML可能不安全。在本例中,它是格式良好的。xml的结构就是这样设计的。是否有可能删除这些Hi Mads!我现在知道你是XSLT大师了。如果可能的话,你能回答这个问题吗?
<?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>