gpt4 book ai didi

xml - 转换内容在 CDATA 中的 xml 元素

转载 作者:数据小太阳 更新时间:2023-10-29 01:44:40 24 4
gpt4 key购买 nike

我有一个如下所示的 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 中做到这一点吗

最佳答案

运行两个转换怎么样。

通过 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>

关于xml - 转换内容在 CDATA 中的 xml 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2067116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com