xml - 通过 XSLT 1.0 提取 CDATA 内容
问题描述
自上周以来,我一直在谷歌搜索,阅读有关该主题的所有相关 Stackoverflow 答案/主题(也来自其他来源),但仍未找到解决方案。
我有以下 XML:
?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<process id="10" name="Process 1" ownerOE="OE1" accountableOE="accounttableOE1" lastUpdate="" delete="aktiv">
<description>
<![CDATA[Some description 1]]>
</description>
<App name="App1" ic-id="100" />
</process>
<process id="20" name="Process 2" ownerOE="OE2" accountableOE="accounttableOE2" lastUpdate="" delete="aktiv">
<description>
<![CDATA[Some description 2]]>
</description>
<App name="App1" ic-id="100" />
</process>
</root>
在我编写的 XSLT 之后:
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<ImportSchemaBase>
<xsl:for-each select="root/process">
<Process>
<Title>
<xsl:value-of select="@name"/>
</Title>
<Description>
<xsl:value-of select="@description"/>
</Description>
<Tech_ID>
<xsl:value-of select="@id"/>
</Tech_ID>
<lastUpdate>
<xsl:value-of select="@lastUpdate"/>
</lastUpdate>
<deleteFlag>
<xsl:value-of select="@delete"/>
</deleteFlag>
</Process>
</xsl:for-each>
</ImportSchemaBase>
</xsl:template>
</xsl:stylesheet>
当前输出:
<ImportSchemaBase>
<Process>
<Title>Process 1</Title>
<Description/>
<Tech_ID>10</Tech_ID>
<lastUpdate/>
<deleteFlag/>
</Process>
<Process>
<Title>Process 2</Title>
<Description/>
<Tech_ID>20</Tech_ID>
<lastUpdate/>
<deleteFlag/>
</Process>
</ImportSchemaBase>
所需的输出将是(从 CDATA 中提取的内容):
<ImportSchemaBase>
<Process>
<Title>Process 1</Title>
<Description>Some description 1</Description>
<Tech_ID>10</Tech_ID>
<lastUpdate/>
<deleteFlag/>
</Process>
<Process>
<Title>Process 2</Title>
<Description>Some description 2</Description>
<Tech_ID>20</Tech_ID>
<lastUpdate/>
<deleteFlag/>
</Process>
</ImportSchemaBase>
我也尝试过“text()”匹配模式,但它没有用,我阅读了不同的答案并尝试了他们的方法:
如何使用 xsl 从 xml 节点获取 CDATA 并转换为新的 XML?
如何在 XSLT 中解析 CDATA 元素内的 XML DOM?
https://community.adobe.com/t5/framemaker/quot-unwrap-quot-cdata-with-xslt/td-p/1237978?page=1
http://vvratha.blogspot.com/2012/11/extracting-cdata-section-using-xslt.html
我需要使用 XSLT 1.0。
感谢您提前的帮助和问候
解决方案
在您的 XML 中,description
是 的子元素,process
而不是属性。要获取其字符串值,您必须更改:
<xsl:value-of select="@description"/>
至:
<xsl:value-of select="description"/>
也许:
<xsl:value-of select="normalize-space(description)"/>
这与 CDATA 无关。
推荐阅读
- react-native - 无法从 AsyncStorage 获取项目
- unity3d - 团结 | 在同一个应用程序中使用 AR 和 VR,但不能同时使用
- jmeter - JMeter:一旦我运行 Jmeter 脚本,服务器代理就会停止
- grails - Grails hasMany关联不保存到连接表
- dependency-injection - 依赖注入的多种实现 - ASP.NET Core
- android - 在应用程序设置中禁用并启用后,Android 提醒通知不显示
- php - Laravel 7.x 迁移更改列问题
- javascript - jQuery $.notify 不是函数错误。我怎样才能解决这个问题?
- linux - 终结者自定义调色板中的颜色何时启动?
- c# - Azure DevOps Pipeline 变量定义在构建之前自动更改