Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 11th, 2010, 03:49 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default match elements with namespace

Hello,
I am using
<xsl:template match="my_element_name">
to get match my XML elements, the problem is that the elements all have namespaces so the will look like "ab:my_element_name". When I try to match it nothing will happen and I am only able to process the node when I take remove the namespace from the XML file and just process the "my_element_name".

this is the xsd file "extracted and changed"
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                xmlns:fn="http://www.w3.org/2005/xpath-functions" 
                xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" 
                xmlns:err="http://www.w3.org/2005/xqt-errors" 
                xmlns:be="http://some valid address">
	
  <xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
			<xsl:apply-templates/>
	</xsl:template>

  <xsl:template match="my_element_name">
    <!--create the element with its attributes-->
    <xsl:element name="my_new_element_name">
       <xsl:attribute name="my_attrib1">
        <xsl:value-of select="my_element_name2"/> 
       </xsl:attribute>
       <xsl:attribute name="my_attrib2"/>
       <xsl:attribute name="my_attrib3"/>
       
       
       <xsl:element name="my_other_new_element">
        <xsl:apply-templates/>
       </xsl:element>
    </xsl:element>
  </xsl:template>

 
</xsl:stylesheet>

with out namespace
Code:
<?xml version="1.0" encoding="UTF-8"?>
<my_element_name identifier="">
  <my_element_name2>some stuff</my_element_name2>
  .....
  .....
</my_element_name>
with namespace

Code:
<?xml version="1.0" encoding="UTF-8"?>
<ab:my_element_name identifier="" xmlns:ab="some address" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="some address">
  <ab:my_element_name2>be:soknadid</ab:my_element_name2>
  ....
  .....
  
  </ab:my_element_name>
why can't I process the elements with namespaces?

thanks,
es
Reply With Quote
  #2 (permalink)  
Old October 11th, 2010, 03:57 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You simply need to use the namespace to match the element as well:

Code:
<xsl:template match="ab:my_element_name">
where "ab" is a namespace prefix that matched the namespace on the input XML document.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #3 (permalink)  
Old October 11th, 2010, 05:16 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default

This is why I am asking the question here, I did try to match the element wtih the namespace, but for some reason it just didn't find it and automaticlly used the default template to process the xml file. so I don't know is it to do with my namespace declaration or something else.
Reply With Quote
  #4 (permalink)  
Old October 11th, 2010, 05:25 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I did try to match the element wtih the namespace,

But you haven't shown us the code you wrote when you tried this, so we can't tell you where it was wrong.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #5 (permalink)  
Old October 11th, 2010, 05:27 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It is unclear from your examples what the actual namespace in your input XML - I'm not sure that "some address" is a valid namespace.

But whatever your input XML namespace - you simply need to define that in your XSLT and use it as I suggest. The prefix does not need to be the same, but the namespace must match exactly (case sensitivity as well).
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #6 (permalink)  
Old October 11th, 2010, 07:06 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default

sorry for wasting your time, I was using the same address for the namespace and the schemaLocation. But thanks for the replays.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template match should find all elements ending in 'wp' Kenneth.Dougherty XSLT 2 November 23rd, 2009 02:16 PM
Default Namespace does not display elements. AjayLuthria XSLT 2 April 17th, 2007 10:11 AM
How to Only Match Elements that Equal an XSL Var? tclancy XSLT 1 April 21st, 2006 04:05 PM
Elements that don't match any templates clareE XSLT 3 December 21st, 2005 06:20 AM
how to match elements starting with roopa XSLT 1 October 20th, 2005 10:29 AM



All times are GMT -4. The time now is 09:00 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.