我必须使用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后重命名标签吗?我不明白我在这里缺少什么?
提前致谢
1 回答
CDATA中没有元素,只有文本 . 这就是CDATA的意思:“这些东西可能看起来像标记,但我希望它被视为文本” .
将文本转换为元素称为解析,因此要从CDATA中的文本中提取元素,您将不得不对其进行解析 . 在进入XSLT 3.0(具有parse-xml()函数)之前,没有直接的方法在XSLT中执行此操作 . 一些XSLT处理器具有扩展功能;在某些(我相信)exslt:node-set()函数执行此操作,如果您提供一个字符串作为输入 . 与其他人一起,您可以调用自己的Java或Javascript代码来进行解析 . 所以这一切都变得依赖于处理器 .
另一种方法是使用disable-output-escaping技巧在CDATA部分输出XML,然后在第二次转换中处理它 .
最好的方法是在开始之前摆脱CDATA标签 . 它们本来就不应该放在那里 .