472,790 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,790 software developers and data experts.

Matching elements that contain different namespaces

When processing an xml document that contains elements such as:

<element xmlns="goop"/>
...
<element xmlns="gleep"/>

I want to create two xsl stylesheet templates: one that matches
the elements that contain the namespace declaration for "goop"
and one that matches the elements that contain the namespace
declaration for "gleep".

How should the match patterns be written?

Thanks!

-Hugh Sparks, hu**@csparks.com
Jul 20 '05 #1
8 1908

<xsl:template match="a:element">
....

<xsl:template match="b:element">
....
together with

xmlns:a="goop"
xmlns:b="gleep"

either on those selments, or more typically on the xsl:stylesheet
element, so it is in scope for the whole stylesheet.

Note you have to do this even if you only had one namespace, unprefixed
names in XPathe xpressions and XSLT patterns always refer to no-namespace.

David
Jul 20 '05 #2
"David Carlisle" <da****@nag.co.uk> wrote in message
news:yg*************@penguin.nag.co.uk...
[...How to match elements from different namespaces...] <xsl:template match="a:element">
...
<xsl:template match="b:element">
...
together with

xmlns:a="goop"
xmlns:b="gleep"
Thank you: That is a very clear answer.
I simplified too much. My problem is a little more bizarre.

I'm not even sure if this is legal xml, but this is what I'm trying to deal
with:

My document contains elements like this:

<document xmlns="MyStuff">
....
<fe:fragment xmlns="goop" xmlns:fe="Fragment" fragment-id="123"/>
....
<fe:fragment xmlns="gleep" xmlns:fe="Fragment" fragment-id="456"/>
....
</document>

The stylesheet root element looks like this:

<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fe="Fragment"


I want to write one template that matches the fe:fragment that contains
xmlns="goop" and one template that matches the fe:fragment the contains
the xmlns="gleep".

The fe:fragments are created by a piece of software I don't entirely
control.
It seems to me that the fe:fragment elements have conflicting namepace
membership. Is this legal XML by itself:

<fe:fragment xmlns:fe="Fragment"/>

In other words, can you use a namespace prefix on an element that
contains the prefix definition? I though it could only be used by the
element's children...

I found that my stylesheet didn't work if I didn't define the fe prefix
in the stylesheet root.

And what does this mean:

<root xmlns:fe="Fragment">
<fe:fragment xmlns="goop" xmlns:fe="Fragment"/>
</root>

What is the namespace of fe:fragment?
Is the internal xmlns:fe="Fragment" necessary? Sufficient?
Is this legal xml at all?

In case you're interested in the larger context, the fe:fragments are
created by
by Apache Cocoon's FragmentExtractorTransformer. I have a source document
that I need to extract two different elements for processing by two
different external
Cocoon serializers. When a fragment extractors is configured, you specify
the element name you want to extract and the element's namespace URI.

The intermediate xml document is sent to the stylesheet with the elements
extracted and replaced by the <fe:fragment> elements shown above.
The stylesheet rearranges things a bit and the fragments go back to
the Cocoon pipeline where the original elements get looked up and their
content gets processed.

The only way to distinguish the fragments is by their id attribute and
by the xmlns="blah" they contain. The xmlns="blah" is the namespace
of the extracted element. The fragment does not explicity contain the
name of the element it replaced, which seems like a Bad Thing to me.

I need to handle the fragments differently in the stylesheet depending
on the element they came from. In my case, it happens that each
of the two elements is in a different namespace. So if I can
write the proper template, I can handle the fragments properly.

Thanks,

Hugh Sparks, hu**@csparks.com
Jul 20 '05 #3
In article <jM****************@news01.roc.ny>,
Hugh Sparks <hu**@csparks.com> wrote:
I want to write one template that matches the fe:fragment that contains
xmlns="goop" and one template that matches the fe:fragment the contains
the xmlns="gleep".
Why?
Is this legal XML by itself:

<fe:fragment xmlns:fe="Fragment"/>
Yes.
In other words, can you use a namespace prefix on an element that
contains the prefix definition?
Yes.
I found that my stylesheet didn't work if I didn't define the fe prefix
in the stylesheet root.
It should be sufficient to declare it on the elements that use it.
And what does this mean:

<root xmlns:fe="Fragment">
<fe:fragment xmlns="goop" xmlns:fe="Fragment"/>
</root>

What is the namespace of fe:fragment?


"Fragment".

The xmlns="goop" is useless in this example.

-- Richard
Jul 20 '05 #4

"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:ce***********@pc-news.cogsci.ed.ac.uk...
The xmlns="goop" is useless in this example.


Yes, I suspected that.

Just out of curiosity, is the extra xmlns entirely useless?
Wouldn't it apply to the child elements of the fe:fragment?

(I didn't create the fe:fragment elements.
A confounded piece of Cocoon software did that.)

I just want to distinguish the fragments with xslt templates
based on the useless default namespace declarations they
contain. Any ideas?

Thanks,

Hugh Sparks, hu**@csparks.com
Jul 20 '05 #5


Hugh Sparks wrote:
"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:ce***********@pc-news.cogsci.ed.ac.uk...

The xmlns="goop" is useless in this example.

Yes, I suspected that.

Just out of curiosity, is the extra xmlns entirely useless?
Wouldn't it apply to the child elements of the fe:fragment?


It sets a default namespace for the child elements so if there are any
child elements without a prefix in their name then that xmlns="goop"
would indeed apply to them. But you posted
<fe:fragment xmlns="goop" xmlns:fe="Fragment" fragment-id="123"/>
which is an empty element so in that case there are no child elements
and the xmlns="goop" is useless.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #6
In article <AB*****************@news01.roc.ny>,
Hugh Sparks <hu**@csparks.com> wrote:
Just out of curiosity, is the extra xmlns entirely useless?
Wouldn't it apply to the child elements of the fe:fragment?


Yes, the reason I said it was useless was that there were no children
in the example.

-- Richard
Jul 20 '05 #7
In article <AB*****************@news01.roc.ny>,
Hugh Sparks <hu**@csparks.com> wrote:
I just want to distinguish the fragments with xslt templates
based on the useless default namespace declarations they
contain. Any ideas?


The xmlns declarations themselves don't appear in the XPath data model,
so you can't test for them directly.

But the resulting in-scope-namespaces *do* appear in the data model.

This will match any element that has a prefix or the default namespace
bound to "goop":

*[namespace::* = 'goop']

If you insist on it being the default namespace it's a bit trickier:

*[namespace::*[name()=''] = 'goop']

Those will also match descendants of the elements with the
xmlns="goop" declaration, unless they have xmlns= declarations of
their own. If you only want to match the top-level ones, use

*[namespace::* = 'goop'][not(../namespace::* = 'goop')]

-- Richard
Jul 20 '05 #8
> Hugh Sparks <hu**@csparks.com> wrote:
I just want to distinguish the fragments with xslt templates
based on the useless default namespace declarations they
contain. Any ideas?


"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:ce***********@pc-news.cogsci.ed.ac.uk... In article <AB*****************@news01.roc.ny>,
This will match any element that has a prefix or the default namespace
bound to "goop":

*[namespace::* = 'goop']

[...More profound stuff...]


So to distinguish only my cocoon fragments, the templates would be:

<xsl:template match="fe:fragment[namespace::*='goop']">
...
</xsl:template>
<xsl:template match="fe:fragment[namespace::*='gleep']">
...
</xsl:template>

It works!!

I'm going to ask the author of the FragmentExtractorTransformer about
adding a fragment-name attribute with a value that contains the replaced
element name. That would avoid this obscure difficulty. It's not always
possible for the extracted elements to come from different namespaces.

Thanks very much,

Hugh Sparks, hu**@csparks.com
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Shannon Hughes | last post by:
I created a simple 7 line stylesheet in XML Spy that is matching certain elements (table names) used to created some SQL syntax in the result file. I had to do this for 4 different Elements so I...
0
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?>...
1
by: Lothar Lemnitzer | last post by:
Dear fellows, I have a problem with matching namespaces in an XML document and an XSLT stylesheet (at least I think that this is the source of trouble). My XML document looks like this: 1...
8
by: MENTAT | last post by:
Hi Guys, Newbie question here. Part of my xml looks like this. <node> <description/> <config/> <log/> <transition/> <node>
4
by: David S. Alexander | last post by:
I am trying to transform XML to XML using an XSLT in C#, but the root node of my XML is not being matched by the XSLT if it has an xmlns attribute. Am I handling my namespaces incorrectly? My C#...
11
by: solandre | last post by:
hi there, i have to find matches in two sorted arrays. there are two good possibilities as a temporary solution, performing different, depending on the inner structure of the data. but i try to...
3
by: dmc | last post by:
Can anyone suggest an elegant solution for this problem? I have a source document as follows: <Object Type="Type1"> <Attribute_A>ValueA</Attribute_A> <Attribute_B>ValueB</Attribute_B>...
3
by: luthriaajay | last post by:
I have a query regarding mapping of 2 different namespace elements : 1).I get the undermentioned XML data from a system: <?xml version="1.0" encoding="UTF-8"?> <FIXML...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.