XSL转换错误-未定义命名空间前缀

时间:2019-07-01 12:42:03

标签: xml xslt xsl-stylesheet

我正在尝试使用XSL转换来转换此XML文件:https://gist.github.com/mleontenko/d83026d2a02bedeb7531881144e345aa

我正在使用XSL文件向现有代码添加新的XML代码段。 XSL文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Identity template, copies everything as is -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Override for target element -->
  <xsl:template match="gmd:CI_Citation">
    <!-- Copy the element -->
    <xsl:copy>
      <!-- And everything inside it -->
      <xsl:apply-templates select="@* | *"/> 
      <!-- Add new node (or whatever else you wanna do) -->
      <!-- <xsl:element name="newNode"/> -->
      <gmd:identifier>
          <gmd:RS_Identifier>
             <gmd:code>
                <gco:CharacterString>0105</gco:CharacterString>
             </gmd:code>
             <gmd:codeSpace>
                <gco:CharacterString>hr:nipp:hr</gco:CharacterString>
             </gmd:codeSpace>
             <gmd:version>
                <gco:CharacterString>1.0</gco:CharacterString>
             </gmd:version>
          </gmd:RS_Identifier>
       </gmd:identifier>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

我在浏览器中遇到以下错误(未定义[element]上的命名空间前缀[prefix]): enter image description here

我该如何解决?

1 个答案:

答案 0 :(得分:2)

该消息告诉您尚未定义名称空间前缀。这是指XSLT中出现的gmd:gco:前缀。

它们是在您的XML中定义的。...

<gmd:MD_Metadata xmlns:gmd="http://www.isotc211.org/2005/gmd" 
                 xmlns:gco="http://www.isotc211.org/2005/gco"

因此,您只需要在XSLT中添加类似的定义即可识别它们

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:gmd="http://www.isotc211.org/2005/gmd" 
    xmlns:gco="http://www.isotc211.org/2005/gco">
相关问题