<xsl:template match="p"> not working

Questions about XML that are not covered by the other forums should go here.
sderrick
Posts: 239
Joined: Sat Jul 10, 2010 4:03 pm

<xsl:template match="p"> not working

Post by sderrick » Sun Dec 12, 2010 10:26 pm

I'm writing an xslt transform to number all the <p> tags in a xml document

My initial script below

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xd"
version="1.0">

<xsl:template match="p">
<p><xsl:apply-templates select="node() | @*"/>
</p>
</xsl:template>

<xsl:template match="node() | @*" priority="-1">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

However the template match="p" never is entered?

If I add the TEI namespace
xmlns:tei="http://www.tei-c.org/ns/1.0"

and change the template to match="tei:p" it works,

but then it adds the namespace tags to all the initial <p> <s> and <lb> tags which is bad.

Isn't there a way to match the <p> tags without using the tei namespace, or suppress the namespace info in the output file?

Radu
Posts: 7028
Joined: Fri Jul 09, 2004 5:18 pm

Re: <xsl:template match="p"> not working

Post by Radu » Mon Dec 13, 2010 10:43 am

Hi,

An XPath which is intended to match an XML element from a namespace must also take the namespace into account when selecting (usually by defining a prefix for it).
In XSLT 2.0 you can add an attribute to the xsl:stylesheet like:
xpath-default-namespace="http://www.tei-c.org/ns/1.0"
so that when the XSLT processor finds unprefixed elements in the XPath expressions it will consider them to be in the default namespace.
You also have to set the default prefix to the TEI namespace:
xmlns="http://www.tei-c.org/ns/1.0"

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xpath-default-namespace="http://www.tei-c.org/ns/1.0"
xmlns="http://www.tei-c.org/ns/1.0">
<xsl:template match="p">
<p><xsl:apply-templates select="node() | @*"/>
</p>
</xsl:template>

<xsl:template match="node() | @*" priority="-1">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
To process an XSLT 2.0 with Oxygen you have to use the Saxon 9 XSLT (HE, PE or EE) processor in the associated transformation scenario

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply