首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT-无法提取给定节点的子节点。

XSLT-无法提取给定节点的子节点。
EN

Stack Overflow用户
提问于 2020-04-14 15:50:58
回答 3查看 149关注 0票数 0

我有以下XML文件,并希望提取此XML文件的某些部分。我试过不同的xslts,但我没有工作。我不明白我在哪里犯了一个错误。

示例文件

代码语言:javascript
复制
   <GetDataResponse xmlns="http://test">
         <GetDataResult>
            <Classification>TEST</Classification>
            <data>
               <ItemList xmlns="">
                  <Item>
                     <CommonMetadata>
                        <InfoCoreId>6719d364-7145-4f60-b5c2-07c38a29cce5</InfoCoreId>
                        <FirstReceivedTimestamp>2020-03-31T12:29:51.977Z</FirstReceivedTimestamp>
                        <LastUpdatedTimestamp>2020-03-31T12:29:51.977Z</LastUpdatedTimestamp>
                        <CacheExpiresOnTimestamp>2020-04-28T12:29:51.977Z</CacheExpiresOnTimestamp>
                        <IsDeleted>False</IsDeleted>
                        <ADSName>Test</ADSName>
                        <ADSURI/>
                        <ADSInfoCoreId>885bc303-13b2-48e2-8186-8d60cb0ceecf</ADSInfoCoreId>
                        <IngestionName>Test</IngestionName>
                        <IngestionId>bd0c753e-5262-48cd-b946-7f3a1220ac31</IngestionId>
                        <Description>Test</Description>
                        <SourceAssignedId/>
                        <SourceChecksum/>
                        <Author/>
                        <Geography>
                           <KMLRepresentation>
                              <kml:kml xmlns:kml="http://www.opengis.net/kml/2.2">
                                 <kml:Placemark>
                                    <kml:description>TEST</kml:description>
                                    <kml:Point>
                                       <kml:coordinates>111111,22222</kml:coordinates>
                                    </kml:Point>
                                 </kml:Placemark>
                              </kml:kml>
                           </KMLRepresentation>
                        </Geography>
                        <BSO/>
                        <ModeOfOperation>Live</ModeOfOperation>
                     </CommonMetadata>
                  </Item>
               </ItemList>
            </data>
            <xsltOutputFormat/>
         </GetDataResult>
      </GetDataResponse>

我想得到“数据”元素节点下的所有节点及其值,如下所示;

预期文件

代码语言:javascript
复制
              <ItemList xmlns="">
                  <Item>
                     <CommonMetadata>
                        <InfoCoreId>6719d364-7145-4f60-b5c2-07c38a29cce5</InfoCoreId>
                        <FirstReceivedTimestamp>2020-03-31T12:29:51.977Z</FirstReceivedTimestamp>
                        <LastUpdatedTimestamp>2020-03-31T12:29:51.977Z</LastUpdatedTimestamp>
                        <CacheExpiresOnTimestamp>2020-04-28T12:29:51.977Z</CacheExpiresOnTimestamp>
                        <IsDeleted>False</IsDeleted>
                        <ADSName>Test</ADSName>
                        <ADSURI/>
                        <ADSInfoCoreId>885bc303-13b2-48e2-8186-8d60cb0ceecf</ADSInfoCoreId>
                        <IngestionName>Test</IngestionName>
                        <IngestionId>bd0c753e-5262-48cd-b946-7f3a1220ac31</IngestionId>
                        <Description>Test</Description>
                        <SourceAssignedId/>
                        <SourceChecksum/>
                        <Author/>
                        <Geography>
                           <KMLRepresentation>
                              <kml:kml xmlns:kml="http://www.opengis.net/kml/2.2">
                                 <kml:Placemark>
                                    <kml:description>TEST</kml:description>
                                    <kml:Point>
                                       <kml:coordinates>111111,22222</kml:coordinates>
                                    </kml:Point>
                                 </kml:Placemark>
                              </kml:kml>
                           </KMLRepresentation>
                        </Geography>
                        <BSO/>
                        <ModeOfOperation>Live</ModeOfOperation>
                     </CommonMetadata>
                  </Item>
               </ItemList>

应用XSLT-1

代码语言:javascript
复制
<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="GetDataResponse/GetDataResult/data">

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

 </xsl:template>
</xsl:stylesheet>

应用XSLT-2

代码语言:javascript
复制
<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="GetDataResponse/GetDataResult/data">

    <xsl:copy-of select="node()"/>

 </xsl:template>
</xsl:stylesheet>

但这些都不适合我。我的xslt有什么问题?

谢谢你的帮助。

EN

回答 3

Stack Overflow用户

发布于 2020-04-14 16:11:26

我认为您应该检查输入文件中使用的名称空间。

这是一个你想做的解决方案。请注意,您不能忽略名称空间,在处理节点时必须考虑到它。

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tst="http://test">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="tst:GetDataResponse/tst:GetDataResult/tst:data"/>
    </xsl:template>

     <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>

</xsl:stylesheet>

看到它在这里工作:https://xsltfiddle.liberty-development.net/gVhDDyS

票数 2
EN

Stack Overflow用户

发布于 2020-04-14 17:05:54

没有名称空间的解决方案:

代码语言:javascript
复制
<?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"/>

<xsl:template match="*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

 <xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

<xsl:template match="/">
    <xsl:copy>
        <xsl:apply-templates select="//ItemList"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/jxDiMBU

票数 1
EN

Stack Overflow用户

发布于 2020-04-14 17:26:47

或者简单地说:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <xsl:copy-of select="//ItemList"/>
</xsl:template>

</xsl:stylesheet>

尽管就我个人而言,我更喜欢一个更明确的路径:

代码语言:javascript
复制
<xsl:copy-of select="*/*/*/ItemList"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61211845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档