XSL命名空间误解

时间:2013-06-24 01:31:13

标签: xslt namespaces

我只是将我的脚趾浸入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>

出于某种原因,我从未进入过&#34; CgPoint&#34;模板(我永远不会得到&#34;在CgPoints&#34; html输出中)。我尝试了各种各样的组合&#34; LandXML / CgPoints / CgPoint&#34;,&#34; CgPoints / CgPoint&#34;等等在调用和模板定义中。

有没有人能够深入了解我的误解源于何处?

提前致谢,

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

通常,您可以使用名称空间前缀在XSLT中声明名称空间,并使用它在@select@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>