xmlxsltxsl-stylesheet

XSL transformation error - Namespace prefix is not defined


I'm trying to transform this XML file using XSL transformation: https://gist.github.com/mleontenko/d83026d2a02bedeb7531881144e345aa

I'm using XSL file to add a new XML snippet to existing code. The XSL file looks like this:

<?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>

I get the following error in my browser (Namespace prefix [prefix] on [element] is not defined): enter image description here

How can I solve this?


Solution

  • The message is telling you namespace prefixes have not defined. This is refering to the gmd: and gco: prefixes that occur in your XSLT.

    They are defined in your XML....

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

    So, you just need to add similar definitions to your XSLT for it to recognise them

    <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">