How to pull the namespaces to the serailized XML Fragment using Xalan-C++ API

L

lanegroups

Dear experts,
I'm trying to do the following using Xalan-C++ API
1. parse an XML document
2. evaluate an XML path
3. serialize the selected XML subtree
4. The new serialized XML fragment should have the correct namespace
declared, based on their declaration from the original XML document.

Task 1-3 are done and I'm using the traverseSubtree method from
FormatterTreeWalker class to serialize the subtree. However, the
namespace declaration is not put in the new root element by the
traverseSubtree method.
For example,
<doc xmlns:ns1="http://www.one.com"
xmlns:ns2="http://www.two.com"
xmlns:ns3="http://www.three.com"
xmlns="http://www.default.com">
<ns1:element1>
<ns2:element2>
<ns3:element3>123</ns3:element3>
</ns2:element2>
</ns1:element1>
</doc>
After evaluating xpath
"'/default:doc/ns1:element1/ns2:element2/ns3:element3"
I can get the serialized fragment as
<ns3:element3>123</ns3:element3>

I want to get something below
<ns3:element3 xmlns:ns3="http://www.three.com"
xmlns="http://www.default.com">123</ns3:element3>

or

<ns3:element3 xmlns:ns1="http://www.one.com"
xmlns:ns2="http://www.two.com"
xmlns:ns3="http://www.three.com"
xmlns="http://www.default.com">123</ns3:element3>

I think I can travese the tree again to collect the namespace URIs and
prefixes and generate the string in the root, but that's not effecient.

Are there existing Xalan C++ APIs that can handle this already?
For example, are there Xalan C++ APIs that I can retrieve the related
namespace URIs and prefixes based on the context?

Thanks a lot in advance!!
 
J

Joseph Kesselman

Namespace declarations either have to be copied across explicitly, or
you have to use a serializer that perfoms namespace fixup, or both.

Xalan's stylesheet code does the former when "namespace nodes" are
copied, and its serializer does the latter when necessary.

If you're wrapping different code around the XPath calls, it's your
responsibility to make sure the right things happen. Borrowing Xalan's
serializer might be the simplest solution.
 
J

Joe Kesselman

Lan said:
> What I miss is the part copying "namespace nodes". Can you elaborate
> more on the stylesheet code that copy the "namespaces nodes"?

Namespace Nodes are an XSLT/XPath concept. Basically, they're the
declarations in scope at a given node. The normal
copy-a-node-from-source-to-result mechanisms in XSLT (xsl:copy)
conceptually copies these along with the nodes, helping to ensure not
only that all namespaces used in node names are properly defined, but
that those in text content (for example, in XPaths that are part of the
content being processed) have their necessary info.

See the XPath spec's description of the namespace:: axis, and XSLT's
description of how namespace nodes are handled.

If you're hand-coding the copy process, it's up to you to either do this
sort of copying -- determine which declarations are in scope and
explicitly declare them in the output document -- or to have your
serializer regenerate them when you discover that a namespaced node has
a prefix that isn't already declared with that value at this point in
the document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top