0

我正在尝试使用 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]): 在此处输入图像描述

我该如何解决这个问题?

4

1 回答 1

3

该消息告诉您名称空间前缀尚未定义。这是指出现在 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">
于 2019-07-01T13:05:31.250 回答