首页 > 解决方案 > 通过 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?

如何使用 xsl 从 xml 节点获取 CDATA?

如何在 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。

感谢您提前的帮助和问候

标签: xmlxsltxslt-1.0

解决方案


在您的 XML 中,description是 的子元素process而不是属性。要获取其字符串值,您必须更改:

<xsl:value-of select="@description"/>

至:

<xsl:value-of select="description"/>

也许:

<xsl:value-of select="normalize-space(description)"/>

这与 CDATA 无关。


推荐阅读