使用xslt从xml中提取值

时间:2018-03-08 08:40:31

标签: xml xslt

我有以下xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<wcs:CoverageDescriptions xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0" xmlns:ows="http://www.opengis.net/ows/2.0" xmlns:swe="http://www.opengis.net/swe/2.0" xmlns:wcs="http://www.opengis.net/wcs/2.0" xmlns:wcsgs="http://www.geoserver.org/wcsgs/2.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.opengis.net/wcs/2.0 http://schemas.opengis.net/wcs/2.0/wcsDescribeCoverage.xsd">
    <wcs:CoverageDescription gml:id="AOI" xmlns="http://www.opengis.net/gml/3.2" xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0" xmlns:swe="http://www.opengis.net/swe/2.0">
        <boundedBy>
            <Envelope axisLabels="DATE E N" srsDimension="3" srsName="http://localhost:8080/def/crs-compound?1=http://localhost:8080/def/crs/OGC/0/UnixTime?axis-label=&quot;DATE&quot;&amp;2=http://localhost:8080/def/crs/EPSG/0/32630" uomLabels="s metre metre">
                <lowerCorner>"2012-05-09T00:00:00.000Z" 210417 4096350</lowerCorner>
                <upperCorner>"2013-11-28T00:00:00.000Z" 230417 4116350</upperCorner>
            </Envelope>
        </boundedBy>
        <wcs:CoverageId>AOI</wcs:CoverageId>
        <coverageFunction>
            <GridFunction>
                <sequenceRule axisOrder="+1 +3 +2">Linear</sequenceRule>
                <startPoint>0 0 0</startPoint>
            </GridFunction>
        </coverageFunction>
    </wcs:CoverageDescription>
</wcs:CoverageDescriptions>

我想在标签lowerCorner(以及upperCorner)和sequenceRule中提取信息。 我正在使用以下xsl文件:

<?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="/">
        <!-- TODO: Auto-generated template -->
        <!-- <?xml version="1.0" encoding="UTF-8"?> -->
<gmi:MI_Metadata xmlns:gmi="http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" xmlns:gss="http://www.isotc211.org/2005/gss" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" gco:isoType="gmd:MD_Metadata" xsi:schemaLocation="http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi/gmi.xsd" xmlns:wcs="http://www.opengis.net/wcs/2.0" xmlns:wcsgs="http://www.geoserver.org/wcsgs/2.0" xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0" xmlns:ows="http://www.opengis.net/ows/2.0" xmlns:swe="http://www.opengis.net/swe/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">

<gmd:extent>
    <gmd:EX_Extent>
        <gmd:temporalElement>
            <gmd:EX_TemporalExtent>
                <gmd:extent>
                    <gml:TimePeriod gml:id="d1e468a1049886">
                        <gml:beginPosition>
                            <xsl:for-each select="//Envelope">
                                <xsl:variable name="my_lowerCorner" select="substring-before(lowerCorner, ' ')" />  
                                <xsl:value-of select="substring($my_lowerCorner,1,19)" />
                            </xsl:for-each>

                            <covFunction><xsl:value-of select="//coverageFunction/GridFunction/sequenceRule"/><\covFunction>
                            </gml:beginPosition>
                    </gml:TimePeriod>
                </gmd:extent>
            </gmd:EX_TemporalExtent>
        </gmd:temporalElement>
    </gmd:EX_Extent>
</gmd:extent>
</gmi:MI_Metadata>
</xsl:template>
</xsl:stylesheet>

但我得到空的输出。你能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

<CoverageDescription>关联的默认命名空间<wcs:CoverageDescription gml:id="AOI" xmlns="http://www.opengis.net/gml/3.2" xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0" xmlns:swe="http://www.opengis.net/swe/2.0"> 。此命名空间未映射到XSLT中。

xmlns:tmp="http://www.opengis.net/gml/3.2"

在XSLT <xsl:for-each select="//tmp:Envelope"> <xsl:value-of select="substring(substring-before(tmp:lowerCorner, ' '),1,19)" /> </xsl:for-each> <covFunction> <xsl:value-of select="//tmp:coverageFunction/tmp:GridFunction/tmp:sequenceRule" /> </covFunction> 中映射名称空间并正确关联元素后,将在输出中接收值。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:gmi="http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml"
            xmlns:gss="http://www.isotc211.org/2005/gss" xmlns:gco="http://www.isotc211.org/2005/gco"
            xmlns:gmd="http://www.isotc211.org/2005/gmd" gco:isoType="gmd:MD_Metadata"
            xsi:schemaLocation="http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi/gmi.xsd"
            xmlns:wcs="http://www.opengis.net/wcs/2.0" xmlns:wcsgs="http://www.geoserver.org/wcsgs/2.0"
            xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0" xmlns:ows="http://www.opengis.net/ows/2.0"
            xmlns:swe="http://www.opengis.net/swe/2.0" xmlns:xlink="http://www.w3.org/1999/xlink"
            xmlns:tmp="http://www.opengis.net/gml/3.2">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="/">
        <gmi:MI_Metadata >
            <gmd:extent>
                <gmd:EX_Extent>
                    <gmd:temporalElement>
                        <gmd:EX_TemporalExtent>
                            <gmd:extent>
                                <gml:TimePeriod gml:id="d1e468a1049886">
                                    <gml:beginPosition>
                                        <xsl:for-each select="//tmp:Envelope">
                                            <xsl:value-of select="substring(substring-before(tmp:lowerCorner, ' '),1,19)" />
                                        </xsl:for-each>
                                        <covFunction>
                                            <xsl:value-of select="//tmp:coverageFunction/tmp:GridFunction/tmp:sequenceRule" />
                                        </covFunction>
                                    </gml:beginPosition>
                                </gml:TimePeriod>
                            </gmd:extent>
                        </gmd:EX_TemporalExtent>
                    </gmd:temporalElement>
                </gmd:EX_Extent>
            </gmd:extent>
        </gmi:MI_Metadata>
    </xsl:template>
</xsl:stylesheet>

以下是完整的XSLT。

<gmi:MI_Metadata xmlns:gco="http://www.isotc211.org/2005/gco"
    xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:swe="http://www.opengis.net/swe/2.0"
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gss="http://www.isotc211.org/2005/gss"
    xmlns:tmp="http://www.opengis.net/gml/3.2" xmlns:wcs="http://www.opengis.net/wcs/2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows/2.0"
    xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0" xmlns:gml="http://www.opengis.net/gml"
    xmlns:gmi="http://sdi.eurac.edu/metadata/iso19139-2/schema/gmi"
    xmlns:wcsgs="http://www.geoserver.org/wcsgs/2.0">
    <gmd:extent>
        <gmd:EX_Extent>
            <gmd:temporalElement>
                <gmd:EX_TemporalExtent>
                    <gmd:extent>
                        <gml:TimePeriod gml:id="d1e468a1049886">
                            <gml:beginPosition>
                                "2012-05-09T00:00:0
                                <covFunction>Linear</covFunction>
                            </gml:beginPosition>
                        </gml:TimePeriod>
                    </gmd:extent>
                </gmd:EX_TemporalExtent>
            </gmd:temporalElement>
        </gmd:EX_Extent>
    </gmd:extent>
</gmi:MI_Metadata>

输出

code=H14 desc="No web processes running"