转换内容在CDATA中的xml元素
我有一个如下所示的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="
<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,'<div class="heading">')" />
<xsl:variable name="afteropen" select="substring-after(//Detail,'<div class="heading">')" />
<xsl:variable name="body" select="substring-before($afteropen, '</div>')" />
<xsl:variable name="after" select="substring-after($afteropen, '</div>')" />
<xsl:value-of select="concat($before, '<h1>', $body, '</h1>',$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>