【发布时间】:2016-08-22 14:32:59
【问题描述】:
我必须使用 XSLT 转换我的输入 xml。 它包含 CDATA,我需要从 CDATA 中提取元素,然后我必须重命名标签。
以下是我的输入 xml:
<getArtifactContentResponse>
<return>
<![CDATA[
<metadata>
<overview>
<name>scannapp</name>
<developerId>developer702</developerId>
<stateId>2</stateId>
<serverURL>dddd</serverURL>
<id>cspapp1103</id>
<description>scann doc</description>
<hostingTypeId>1</hostingTypeId>
</overview>
</metadata>
]]>
</return>
</getArtifactContentResponse>
预期的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<metadata >
<information>
<name>scannapp</name>
<developerId>developer702</developerId>
<stateId>2</stateId>
<serverURL>ddddd</serverURL>
<id>cspapp1103</id>
<description>scann doc</description>
<hostingTypeId>1</hostingTypeId>
</Information>
</metadata>
我正在使用的 XSLT 如下:
<xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:template match="/">
<xsl:value-of select="//ns:getArtifactContentResponse/ns:return/text()" disable-output-escaping="yes"/>
</xsl:template>
<xsl:template match="overview">
<Information>
<xsl:apply-templates select="@* | node()" />
</Information>
</xsl:template>
有了这个,我可以提取 CDATA,但它不会将元素 'overview' 重命名为 'Information' 。
转换后的xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<overview>
<name>scannapp</name>
<developerId>developer702</developerId>
<stateId>2</stateId>
<serverURL>dddddd</serverURL>
<id>cspapp1103</id>
<description>scann doc</description>
<hostingTypeId>1</hostingTypeId>
</overview>
</metadata>
谁能告诉我如何在提取 CDATA 后重命名标签? 我不明白我在这里缺少什么?
提前致谢
【问题讨论】:
-
如果您只有 XSLT 1.0 版,您需要在初始 XSLT 的输出上运行另一个 XSLT 以产生所需的输出。 CData 的内容被认为是纯文本,因此不能像普通 XML 片段一样被 XSLT 处理。
-
我不清楚。你能用上面的例子解释一下吗?
-
Link no.1 展示了 2 变换方法。您已经拥有用于第一个转换步骤的 XSLT。现在创建另一个 XSLT 将第一个转换的输出转换为最终的预期输出...
-
现在,我有两个 xslt。但是我可以按顺序应用这两个 xslt 吗?我不应该明确使用两个 xsl 文件。
标签: xslt rename cdata extraction