Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 11th, 2010, 07:23 AM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT apply-templates namespace problem

dear forum users,

i hope someone can help me with the following problem:

i want to apply an XSLT transformation on an XSD document. my problem is now that the "xsl:apply-templates" element does not return any matching node unless i define the xpath expression as an xsl:variable.

concretely lets look at this example XSD:

<xsd:schema xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.gbxml.org/schema" targetNamespace="http://www.foo.org/schema" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="0.37">
<xsd:annotation>
<xsd:documentation>documentation of a problem</xsd:documentation>
</xsd:annotation>
</xsd:schema>

if i want a rule to give me the value of for example the "targetNamespace" value i could write a template rule in my XSLT stylesheet:

<xsl:template match="xsd:schema" mode="class">
<xsl:value-of select="./@targetNamespace"/>
</xsl:template>

and call this template rule in the global rule:

<xsl:template match="/">
<xsl:apply-templates select="xsd:schema" mode="class"/>
</xsl:template>

however THIS does not work!the matching rule does not return nothing.

i do not understand.. maybe it has a problem with the xsd: namespace (i also declared it in my XSLT stylesheet, of course) but i just figured out that the following soultion by interposing a xsl:variable works:

<xsl:template match="/">
<xsl:variable name="xpath" select="xsd:schema"/>
<xsl:apply-templates select="$xpath" mode="class"/>
</xsl:template>

can someone PLEASE tell me why it shows me this behaviour, and maybe how to omit the usage of an intermediary xsl:variable element.

thank you very much,

best regards,

mario
 
Old March 11th, 2010, 07:34 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Creating a stylesheet out of the bits of XSLT you have posted the above works fine for me (it outputs "http://www.foo.org/schema").

Perhaps if you show us a complete sample XSLT which doesn't work we can try and identify the problem.

What XSLT processor are you using? Perhaps that is an issue?

The following running in Saxon 9 Basic using Kernow for Saxon:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                version="1.0">
	
   <xsl:template match="xsd:schema" mode="class">
      <xsl:value-of select="./@targetNamespace"/>
   </xsl:template>

   <xsl:template match="/">
      <xsl:apply-templates select="xsd:schema" mode="class"/>
   </xsl:template>

</xsl:stylesheet>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 11th, 2010, 09:23 AM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

many thanks for your fast reply.

with your help i figured out what was the problem:

i define a namespace attribute for the target file which is the same namespace attribute as the one i am querying. therefore this attribute is overruling and hiding the original attribute.

concretely:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>

<xsl:template match="/">
<rdf:RDF xmlns="http://www.xxx.com/test#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#">

<xsl:apply-templates select="xsd:schema" mode="class"/>

</rdf:RDF>

</xsl:template>

</xsl:stylesheet>

therefore no node called "schema" was found within the "xsd" namespace defined in the "rdf" tag.

thanks a lot for your help,

best regards,

mario
 
Old March 11th, 2010, 10:05 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

There is nothing to stop you using a different prefix you know. Simply define the one in your stylesheet to be xmlns:myxsd="http://www.w3.org/2001/XMLSchema" and the reference it as select="myxsd:schema".

I find it highly suspicious though that you are redefining the XMLSchema namespace with a # at the end - This isn't the correct namespace for the XML Schema - and yes, that # would cause other applications to fail..

Also, you haven't defined the rdf prefix anywhere.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 11th, 2010, 10:27 AM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello,

yes it is strange, but i want the output document to be a valid "RDF" file. therefore i first created an ontology in Protege and then having a look at the .owl file revealed:

<rdf:RDF xmlns="http://www.sample.com/sample#"
xml:base="http://www.sample.com/sample/sample.owl"
xmlns:BuildingOntology="http://www.sample.com/sample/sample.owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl2xml="http://www.w3.org/2006/12/owl2-xml#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about=""/>

That's the reason why i redefine the namespace with an # at the end, so that it looks this way in the end ;) .

thanks,

mario

Quote:
Originally Posted by samjudson View Post
There is nothing to stop you using a different prefix you know. Simply define the one in your stylesheet to be xmlns:myxsd="http://www.w3.org/2001/XMLSchema" and the reference it as select="myxsd:schema".

I find it highly suspicious though that you are redefining the XMLSchema namespace with a # at the end - This isn't the correct namespace for the XML Schema - and yes, that # would cause other applications to fail..

Also, you haven't defined the rdf prefix anywhere.





Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:apply-templates select problem harapraveen XSLT 2 October 22nd, 2007 08:05 AM
simple XSLT apply-templates question fannus XSLT 7 March 27th, 2007 02:11 AM
apply-templates problem within for-each loop mister_mister XSLT 2 January 22nd, 2007 05:40 PM
Templates Won't Apply neilac333 XSLT 5 October 26th, 2006 01:39 PM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.