Matching elements with namespace prefix in XSLT

2020-02-12 03:52发布

问题:

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.

回答1:

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>


回答2:

Try this to ignore the namespace:

<xsl:template match="*[local-name()='package']">
    <xsl:message>Entering package</xsl:message>
</xsl:template>


回答3:

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>


标签: xslt xslt-1.0