16

This is my xml input.

<package version="2.0" unique-identifier="uuid_id"
         xmlns="http://www.idpf.org/2007/opf">
  <metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:opf="http://www.idpf.org/2007/opf"
            xmlns:dcterms="http://purl.org/dc/terms/"
            xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata"
            xmlns:dc="http://purl.org/dc/elements/1.1/">
    <meta name="calibre:series_index" content="1"/>
    <dc:language>UND</dc:language>
    <dc:creator opf:file-as="Marquez, Gabriel Garcia" 
                opf:role="aut"
               >Gabriel Garcia Marquez</dc:creator>
    <meta name="calibre:timestamp" content="2010-07-14T21:35:15.266000+00:00"/>
    <dc:title>Cem Anos de Solidão</dc:title>
    <meta name="cover" content="cover"/>
    <dc:date>2010-07-14T21:35:15.266000+00:00</dc:date>
    <dc:contributor opf:role="bkp"
                   >calibre (0.7.4) [http://calibre-ebook.com]</dc:contributor>
    <dc:identifier id="uuid_id" opf:scheme="uuid"
                  >7e11dc8b-55cb-4411-8f30-df974fbcf58a</dc:identifier>
  </metadata>
  <manifest>
</package>

and my xslt starts like..

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:template match="package">
     <xsl:message>Entering package</xsl:message>
</xsl:template>

I am using XSLT 1.0 and the template package is not getting matched. When I remove the namespace xmlns="http://www.idpf.org/2007/opf" in package node, the template gets matched. How I can make my template to match without removing the namespaces.

Please help me. Thanks in advance.

ScanQR
  • 3,740
  • 1
  • 13
  • 30
vignesh
  • 1,573
  • 7
  • 33
  • 60
  • possible duplicate of [xslt script doesn't work when a namespace is declared in the root node](http://stackoverflow.com/questions/4964152/xslt-script-doesnt-work-when-a-namespace-is-declared-in-the-root-node) –  Feb 24 '11 at 16:59

3 Answers3

27

Add the namespaces in your stylesheet.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:opf="http://www.idpf.org/2007/opf">

<xsl:template match="opf:package">
     <xsl:message>Entering package</xsl:message>
</xsl:template>
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 6
    Is that the only way of doing it by the way ? Do you have to 'hardcode' the 'xxx:' prefix within the match itself, or is there another option to 'globally' define templates that only cover a particular namespace? – monojohnny Apr 21 '11 at 08:39
10

Try this to ignore the namespace:

<xsl:template match="*[local-name()='package']">
    <xsl:message>Entering package</xsl:message>
</xsl:template>
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Artem Sopin
  • 111
  • 1
  • 2
1

XSLT understands the namespaces QNames which are defined in input XML.

In addition to the above answer we can give any name to our xsl namespace.

Input xml has definition as xmlns:dc="http://purl.org/dc/elements/1.1/" and elements are defines as dc prefix

You can define a stylesheet below :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xhtml="http://www.w3.org/1999/xhtml"
     xmlns:purl="http://purl.org/dc/elements/1.1/"
     xmlns:idpf="http://www.idpf.org/2007/opf">
<xsl:template match="idpf:package/purl:language">
<xsl:message>Entering package. Selected Language.</xsl:message>
</xsl:template>
Mandy
  • 163
  • 1
  • 9