Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching elements with namespace prefix in XSLT

Tags:

xslt

xslt-1.0

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.

like image 921
vignesh Avatar asked Feb 24 '11 05:02

vignesh


People also ask

What is the use of namespace in XSLT?

In XML a namespace is a collection of names used for elements and attributes. A URI (usually, a URL) is used to identify a particular collection of names.

How do you add a namespace in XSLT?

Provided that you're using XSLT 2.0, all you have to do is add the xpath-default-namespace attribute on the document element of each stylesheet: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://example.com"> ...

What is match in XSLT?

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

What is exclude result prefixes in XSLT?

The exclude-result-prefixes attribute in both XSLT 1.0 and XSLT 2.0 allows for the exclusion of certain namespace declarations in the output document.


1 Answers

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>
like image 92
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 17:10

Ignacio Vazquez-Abrams