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

时间:2020-04-14 15:50:58

标签: xml xslt xpath

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

示例文件

   <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>

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

预期文件

              <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

<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

<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有什么问题?

谢谢您的帮助。

3 个答案:

答案 0 :(得分:2)

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

这是一种满足您需求的解决方案。 请注意,您不能忽略名称空间,在进动节点时必须将其考虑在内。

<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

答案 1 :(得分:1)

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

<?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

答案 2 :(得分:1)

或者简单地:

<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>

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

<xsl:copy-of select="*/*/*/ItemList"/>
相关问题