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 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
 
Old October 11th, 2010, 03:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?
 
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.
 
Old October 11th, 2010, 05:25 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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
 
Old October 11th, 2010, 05:27 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?
 
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.





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





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