You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by afrenger <aa...@trilliumsoftware.com> on 2013/09/25 15:29:04 UTC

mapping a prefix to a namespace URI

Hi,

I need to get some values from a number of xml sources that have default
namespaces.  I've implemented the code from the sample SimplXPathAPI and got
this to work if I removed the default namespaces but, if I leave them in,
selectSingleNode returns NULL.  I was able to remove the namespaces for my
test data (or add prefixes), but unfortunately I will not be able to do this
with the real data.
 
I've done an exhaustive search of the xalan-c forums and the internet in
general for the past several days and I've come up with a few things but I
don't think I'm heading in the right direction any longer.  I've implemented
a PrefixResolver derivative (myPrefixResolver) and I'm passing an instance
of that into the XPathEvaluator, so the XPath implementation can map the
namespace prefix to the proper namespace URI.  The problem is that I'm
having trouble mapping the prefix to the namespace.  

Optimally I would like to have a function in my PrefixResolver derivative
that takes a prefix and a uri and adds it to my namespace.

I've come up with the following (and many other variations) but it crashes
when I try to set the node value. 

void
myPrefixResolver::setPrefix(const XalanDOMString& prefix, const
XalanDOMString& uri)
{
	AttributeVectorType	myVector(XalanMemMgrs::getDefaultXercesMemMgr(), 1);
	myVector.front()->setNodeValue(uri);

	m_namespaces.insert(&prefix, myVector);
}

I've tried many things including tying to clone an existing XalanNode and
creating my vector this way.  I haven't deviated much from the way
XalanDocumentPrefixResolver derived from PrefixResolver.  I am a bit
desperate at this point because I have a very tight schedule and I thought
this part would be the easy part.  

Any help that can be proffered would be extremely appreciated!

Thanks,
Aaron







--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/mapping-a-prefix-to-a-namespace-URI-tp40498.html
Sent from the Xalan - C - Users mailing list archive at Nabble.com.

Re: mapping a prefix to a namespace URI

Posted by afrenger <aa...@trilliumsoftware.com>.
Thank you Steven, I think you've given me a solution from a completely
different angle than what I was trying to do.  I will try that tomorrow and
hopefully that will get me by this.

I would still love to figure out how to do it programmatically (it is a
character flaw not being able to let go).  So, if anyone does know how to
add a prefix to a namespace where it is empty in a manner consistent with
the SimpleXPathAPI sample, I would love to understand it.

Thanks again Steven!
Aaron



--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/mapping-a-prefix-to-a-namespace-URI-tp40498p40511.html
Sent from the Xalan - C - Users mailing list archive at Nabble.com.

Re: mapping a prefix to a namespace URI

Posted by sh...@e-z.net.
Aaron,

Without additional C++ programming, and you have an XML document
that associates with xmlns="http://someweb.com" your stylesheet
can assign a prefix (my) to your namespace by using
xmlns:my="http://someweb.com" in your stylesheet or transform
element.

 <xsl:transform
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform
    xmlns:my='http://someweb.com'
    version='1.0'>

and your document can look like:

 <mydoc xmlns="http://someweb.com">
   ... document content
 </modoc>

And the XSLT transformations now know how to use the my: prefix
for your nodes.  If the <xsl:transform> element also has
xmlns="http://someweb.com" then the common XPath functions may
break.

I don't remember what the linkage is to register namespace_names
to the namespace property at this time for XalanTransformer and
XPath runtime configuration structures.

Sincerely,
Steven J. Hathaway

> Thank you very much for your reply Steven.
>
> My xml has xmlns="http://someweb.com" (i.e. no prefix which is what I've
> always understood to be referred to as having default namespace).
>
> If you run a series of XML through the Xalan SimpleXPathAPI sample then,
> if
> you have no name space (no xmlns at all in the doc), you can use XPath
> like
> "/node1/node2/etc" and if you have a defined namespace, you can use XPath
> like /myns:node1/myns:nod2e/myns:etc.  But if you run a document with
> "default namespace"  through the SimpleXPathAPI sample, it returns no node
> found.
>
> I've also found that when you interrogate a document like this in XMLQuire
> it puts out "/dft:node1/dft:node2/dft:etc".
>
> I found a few post where folks had hit this same issue and were told to"
>
> "You'll also need to implement a PrefixResolver derivative and pass an
> instance of that into the XPathEvaluator, so the XPath implementation can
> map the namespace prefix to the proper namespace URI.  You can look at the
> existing PrefixResolver instances to see what's involved."  -Dave Bertoni
>
> http://apache-xml-project.6118.n7.nabble.com/xpath-expression-with-namespace-td22110.html#a22111
>
>
> That post is rather old an maybe I'm heading in the wrong direction as
> there
> may be better ways to do this now.
>
> Thanks again for any help you can give me.
>
> Aaron
>
>
>
> --
> View this message in context:
> http://apache-xml-project.6118.n7.nabble.com/mapping-a-prefix-to-a-namespace-URI-tp40498p40507.html
> Sent from the Xalan - C - Users mailing list archive at Nabble.com.
>
>



Re: mapping a prefix to a namespace URI

Posted by afrenger <aa...@trilliumsoftware.com>.
That's the problem I'm trying to solve, I need to figure out how to map a
prefix to it (programmatically in C++) so I can then use a XPath statement
utilizing the new prefix that Xalan will recognize.

Thanks again,
Aaron



--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/mapping-a-prefix-to-a-namespace-URI-tp40498p40509.html
Sent from the Xalan - C - Users mailing list archive at Nabble.com.

Re: mapping a prefix to a namespace URI

Posted by sh...@e-z.net.
Aaron,

In the case: xmlns="http://someweb.com" == you do have a
namespace associated with the empty prefix.

Steven J. Hathaway

> Thank you very much for your reply Steven.
>
> My xml has xmlns="http://someweb.com" (i.e. no prefix which is what I've
> always understood to be referred to as having default namespace).
>
> If you run a series of XML through the Xalan SimpleXPathAPI sample then,
> if
> you have no name space (no xmlns at all in the doc), you can use XPath
> like
> "/node1/node2/etc" and if you have a defined namespace, you can use XPath
> like /myns:node1/myns:nod2e/myns:etc.  But if you run a document with
> "default namespace"  through the SimpleXPathAPI sample, it returns no node
> found.
>
> I've also found that when you interrogate a document like this in XMLQuire
> it puts out "/dft:node1/dft:node2/dft:etc".
>
> I found a few post where folks had hit this same issue and were told to"
>
> "You'll also need to implement a PrefixResolver derivative and pass an
> instance of that into the XPathEvaluator, so the XPath implementation can
> map the namespace prefix to the proper namespace URI.  You can look at the
> existing PrefixResolver instances to see what's involved."  -Dave Bertoni
>
> http://apache-xml-project.6118.n7.nabble.com/xpath-expression-with-namespace-td22110.html#a22111
>
>
> That post is rather old an maybe I'm heading in the wrong direction as
> there
> may be better ways to do this now.
>
> Thanks again for any help you can give me.
>
> Aaron
>
>
>
> --
> View this message in context:
> http://apache-xml-project.6118.n7.nabble.com/mapping-a-prefix-to-a-namespace-URI-tp40498p40507.html
> Sent from the Xalan - C - Users mailing list archive at Nabble.com.
>
>



Re: mapping a prefix to a namespace URI

Posted by afrenger <aa...@trilliumsoftware.com>.
Thank you very much for your reply Steven.  

My xml has xmlns="http://someweb.com" (i.e. no prefix which is what I've
always understood to be referred to as having default namespace).

If you run a series of XML through the Xalan SimpleXPathAPI sample then, if
you have no name space (no xmlns at all in the doc), you can use XPath like
"/node1/node2/etc" and if you have a defined namespace, you can use XPath
like /myns:node1/myns:nod2e/myns:etc.  But if you run a document with
"default namespace"  through the SimpleXPathAPI sample, it returns no node
found.

I've also found that when you interrogate a document like this in XMLQuire
it puts out "/dft:node1/dft:node2/dft:etc".

I found a few post where folks had hit this same issue and were told to"

"You'll also need to implement a PrefixResolver derivative and pass an 
instance of that into the XPathEvaluator, so the XPath implementation can 
map the namespace prefix to the proper namespace URI.  You can look at the 
existing PrefixResolver instances to see what's involved."  -Dave Bertoni

http://apache-xml-project.6118.n7.nabble.com/xpath-expression-with-namespace-td22110.html#a22111


That post is rather old an maybe I'm heading in the wrong direction as there
may be better ways to do this now.

Thanks again for any help you can give me.

Aaron



--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/mapping-a-prefix-to-a-namespace-URI-tp40498p40507.html
Sent from the Xalan - C - Users mailing list archive at Nabble.com.

Re: mapping a prefix to a namespace URI

Posted by sh...@e-z.net.
Aaron,

When you mention "default" namespace, are you instead
trying to use a "no namespace" arena?

XML documents and fragments with no target namespace prefix
are allocated to the "no namespace" arena.

A namespace_name can be associated with more than one prefix.
The use of any of the prefixes can then validate to a
proper namespace.  Namespaces are bound to prefixes by the
XML Parser.  A Stylesheet can have its own namespace prefix
bindings that are emitted by the XML output method.

Nested namespace (xmlns:prefix='namespace_name') assignments
are allowed.  Parent namespaces that share a prefix that
has been redefined in the child element are not available
within the scope of the child element.

Any namespace (xmlns:prefix) mappings in the parent are
inherited by the child, but can be overwritten by the child
context.

Xalan now has the ability to associate parsed XML documents
with a top-level parameter name.

Documents are parsed using the Xerces-C XML Parser library.

The way a resolver works is to map a URL or system file
pathname to a resource.  The resolver then pushes a reader
onto the XML Reader stack to start reading from the new
object (XML Document or fragment).

The XPath document() function returns a parsed nodeset object.
It uses the installed entity resolver that performs the
above mentioned operation by submitting a bytestream of
data to an XML parser so that a parsed nodeset object can
be returned by document().

Sincerely,
Steven J. Hathaway

> Hi,
>
> I need to get some values from a number of xml sources that have default
> namespaces.  I've implemented the code from the sample SimplXPathAPI and
> got
> this to work if I removed the default namespaces but, if I leave them in,
> selectSingleNode returns NULL.  I was able to remove the namespaces for my
> test data (or add prefixes), but unfortunately I will not be able to do
> this
> with the real data.
>
> I've done an exhaustive search of the xalan-c forums and the internet in
> general for the past several days and I've come up with a few things but I
> don't think I'm heading in the right direction any longer.  I've
> implemented
> a PrefixResolver derivative (myPrefixResolver) and I'm passing an instance
> of that into the XPathEvaluator, so the XPath implementation can map the
> namespace prefix to the proper namespace URI.  The problem is that I'm
> having trouble mapping the prefix to the namespace.
>
> Optimally I would like to have a function in my PrefixResolver derivative
> that takes a prefix and a uri and adds it to my namespace.
>
> I've come up with the following (and many other variations) but it crashes
> when I try to set the node value.
>
> void
> myPrefixResolver::setPrefix(const XalanDOMString& prefix, const
> XalanDOMString& uri)
> {
> 	AttributeVectorType	myVector(XalanMemMgrs::getDefaultXercesMemMgr(), 1);
> 	myVector.front()->setNodeValue(uri);
>
> 	m_namespaces.insert(&prefix, myVector);
> }
>
> I've tried many things including tying to clone an existing XalanNode and
> creating my vector this way.  I haven't deviated much from the way
> XalanDocumentPrefixResolver derived from PrefixResolver.  I am a bit
> desperate at this point because I have a very tight schedule and I thought
> this part would be the easy part.
>
> Any help that can be proffered would be extremely appreciated!
>
> Thanks,
> Aaron
>
>
>
>
>
>
>
> --
> View this message in context:
> http://apache-xml-project.6118.n7.nabble.com/mapping-a-prefix-to-a-namespace-URI-tp40498.html
> Sent from the Xalan - C - Users mailing list archive at Nabble.com.
>
>