1

我想在以下文档中提取 aisStatic bloc(XML 格式):

<Document>
    <numberOfResults>1</numberOfResults>
    <timeCreated>2020-07-17T09:25:12Z</timeCreated>
    <returnCode>200</returnCode>
    <message>OK</message>
    <numVessels>1</numVessels>
    <vessels>
        <aisStatic>
            <name>My beautiful vessel</name>
            <mmsi>0000000000</mmsi>
            <imo>0000000</imo>
            <callsign>XXXX</callsign>
            <flag>Russia</flag>
            <length>140</length>
            <width>17</width>
            <typeOfShipAndCargo>80</typeOfShipAndCargo>
            <aisShiptype>tankships</aisShiptype>
            <updateTime>2018-03-10T14:46:24Z</updateTime>
            <aisClass>A</aisClass>
        </aisStatic>
        <aisVoyage>
            <updateTime>2020-07-17T09:20:34Z</updateTime>
            <eta>2020-07-18T12:00:00Z</eta>
            ...

XSLT 转换:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" method="xml" indent="yes" />
  <xsl:template match="/Document/vessels">
    <xsl:copy-of select="aisStatic"/>
 </xsl:template>
</xsl:stylesheet>

但我也像这样检索 aisStatic 之前节点的值:

    1
    2020-07-17T09:25:12Z
    200
    OK
    1
        <aisStatic>
            <name>My beautiful vessel</name>
            <mmsi>0000000000</mmsi>
            <imo>0000000</imo>
            <callsign>XXXX</callsign>

只获得预期的集团的方法是什么?

4

2 回答 2

0

匹配/并供副本使用select="Document/vessels/aisStatic"

于 2020-07-17T13:21:49.507 回答
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="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="*[not(ancestor-or-self::aisStatic)]"><xsl:apply-templates/></xsl:template>
  <xsl:template match="*[not(ancestor-or-self::aisStatic)]/text()"/>
</xsl:stylesheet>

当此转换应用于提供的 XML(添加结束标记以使其格式良好)文档时:

<Document>
    <numberOfResults>1</numberOfResults>
    <timeCreated>2020-07-17T09:25:12Z</timeCreated>
    <returnCode>200</returnCode>
    <message>OK</message>
    <numVessels>1</numVessels>
    <vessels>
        <aisStatic>
            <name>My beautiful vessel</name>
            <mmsi>0000000000</mmsi>
            <imo>0000000</imo>
            <callsign>XXXX</callsign>
            <flag>Russia</flag>
            <length>140</length>
            <width>17</width>
            <typeOfShipAndCargo>80</typeOfShipAndCargo>
            <aisShiptype>tankships</aisShiptype>
            <updateTime>2018-03-10T14:46:24Z</updateTime>
            <aisClass>A</aisClass>
        </aisStatic>
        <aisVoyage>
            <updateTime>2020-07-17T09:20:34Z</updateTime>
            <eta>2020-07-18T12:00:00Z</eta>
        </aisVoyage>
   </vessels>
</Document>

产生了想要的正确结果

<aisStatic>
    <name>My beautiful vessel</name>
    <mmsi>0000000000</mmsi>
    <imo>0000000</imo>
    <callsign>XXXX</callsign>
    <flag>Russia</flag>
    <length>140</length>
    <width>17</width>
    <typeOfShipAndCargo>80</typeOfShipAndCargo>
    <aisShiptype>tankships</aisShiptype>
    <updateTime>2018-03-10T14:46:24Z</updateTime>
    <aisClass>A</aisClass>
</aisStatic>
于 2020-07-17T18:43:37.287 回答