2

我只是将我的脚趾浸入 XML 和 XSL,并且(从我所读到的,毫不奇怪)我遇到了命名空间的问题。

我得到了一些 XML,我正在尝试编写一些 XSL,但是我在访问一些命名空间和属性等时遇到了问题......

这是我收到的 XML 的摘录...

<?xml version="1.0" encoding="utf-8"?>
<LandXML xmlns="http://www.landxml.org/schema/LandXML-1.2" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="
           http://www.landxml.org/schema/LandXML-1.2 
           http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd" 
         version="1.2" 
         date="2013-06-24" 
         time="01:35:45" 
         readOnly="false" 
         language="English">
  <Units>
    <Metric linearUnit="meter" areaUnit="squareMeter" 
            volumeUnit="cubicMeter" 
            angularUnit="decimal dd.mm.ss" 
            latLongAngularUnit="decimal degrees" 
            temperatureUnit="celsius" 
            pressureUnit="milliBars"/>
  </Units>
  <CoordinateSystem name="HATCH-121204" 
    horizontalDatum="Local" 
    verticalDatum="Local" 
    ellipsoidName="Australian National" 
    projectedCoordinateSystemName="UTM55" 
    fileLocation="">
  </CoordinateSystem>
  <Application name="LandXML Export" 
    manufacturer="Leica Geosystems AG" 
    version="4.3" 
    manufacturerURL="www.leica-geosystems.com">
    <Author company="Leica Geosystems AG" 
      companyURL="www.leica-geosystems.com" 
      timeStamp="2013-06-24T01:35:45"/>
  </Application>
  <CgPoints>
    <CgPoint name="GPS0093" 
             oID="GPS0093" 
             code="728" 
             desc="Tel 
             lines u/g" 
             role="measured" 
             latitude="-21.968945617359" longitude="148.007359609463" 
             ellipsoidHeight="288.420425" 
             timeStamp="2013-06-22T01:55:26"
    >7570087.642555 603892.199974 234.579968</CgPoint>
...
...
...

我正在尝试访问:LandXML/CgPoint/CgPoints,这是我编写的 XSL:

<?xml version="1.0" encoding="UTF-8"?>
<!-- DWXMLSource="130622RLA.xml" -->

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns="http://xml.hexagon.com/schema/HeXML-1.5">
  <xsl:template match="/">
    <html>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <head>
        <title>Field Report</title>
        <link rel="stylesheet" type="text/css" href="books2_xslt.css"/>
        <p class="header">Field Report</p>
      </head>
      <body>
        <xsl:apply-templates select="CgPoints/CgPoint"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="CgPoint">        
    In CgPoints
    <xsl:for-each select="@name">
      <xsl:value-of select="."/>
        HELLO CgPoint
      </xsl:for-each>   
  </xsl:template>

</xsl:stylesheet>

出于某种原因,我从未进入“CgPoint”模板(我从未获得“In CgPoints”html 输出)。我在调用和模板定义中尝试了各种“LandXML/CgPoints/CgPoint”、“CgPoints/CgPoint”等组合。

有没有人知道我的误解来自哪里?

提前致谢,

4

2 回答 2

0

您的元素绑定到命名空间http://www.landxml.org/schema/LandXML-1.2

通常,您会在 XSLT 中使用命名空间前缀声明命名空间,并使用它在@selectand@match条件中对它们进行寻址:

<?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="130622RLA.xml" -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://xml.hexagon.com/schema/HeXML-1.5"
    xmlns:land="http://www.landxml.org/schema/LandXML-1.2">
    <xsl:template match="/">
        <html>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <head>
                <title>Field Report</title>
                <link rel="stylesheet" type="text/css" href="books2_xslt.css"/>
                <p class="header">
                    Field Report</p>
            </head>
            <body>
                <xsl:apply-templates select="*/land:CgPoints/land:CgPoint"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="land:CgPoint">      
        In CgPoints
        <xsl:for-each select="@name">
            <xsl:value-of select="."/>
            HELLO CgPoint
        </xsl:for-each>      
    </xsl:template>

</xsl:stylesheet> 

您还可以对带有谓词过滤器的元素使用通用匹配local-name(): namespace-uri()<xsl:template match="*[local-name()='CgPoint' and namespace-uri()='http://www.landxml.org/schema/LandXML-1.2']">但这非常冗长乏味。

由于您使用的是 XSLT 2.0,因此您还有另外两个选择

1.) 为命名空间使用通配符:

<?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="130622RLA.xml" -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://xml.hexagon.com/schema/HeXML-1.5">
    <xsl:template match="/">
        <html>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <head>
                <title>Field Report</title>
                <link rel="stylesheet" type="text/css" href="books2_xslt.css"/>
                <p class="header">
                    Field Report</p>
            </head>
            <body>
                <xsl:apply-templates select="*/*:CgPoints/*:CgPoint"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="*:CgPoint">      
        In CgPoints
        <xsl:for-each select="@name">
            <xsl:value-of select="."/>
            HELLO CgPoint
        </xsl:for-each>      
    </xsl:template>

</xsl:stylesheet>

2.) 使用xpath-default-namespace(按照 Max Toro 的建议)

<?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="130622RLA.xml" -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://xml.hexagon.com/schema/HeXML-1.5"
    xpath-default-namespace="http://www.landxml.org/schema/LandXML-1.2">
    <xsl:template match="/">
        <html>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <head>
                <title>Field Report</title>
                <link rel="stylesheet" type="text/css" href="books2_xslt.css"/>
                <p class="header">
                    Field Report</p>
            </head>
            <body>
                <xsl:apply-templates select="*/CgPoints/CgPoint"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="CgPoint">      
        In CgPoints
        <xsl:for-each select="@name">
            <xsl:value-of select="."/>
            HELLO CgPoint
        </xsl:for-each>      
    </xsl:template>

</xsl:stylesheet>
于 2013-06-24T02:28:45.743 回答
0

使用xpath-default-namespace

<xsl:stylesheet version="2.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xpath-default-namespace="http://www.landxml.org/schema/LandXML-1.2">
   ...
</xsl:stylesheet>
于 2013-06-24T01:41:32.247 回答