Project

Profile

Help

output escaping in xpath?

Added by Anonymous almost 18 years ago

Legacy ID: #3779974 Legacy Poster: Henry Story (bblfish)

Is there a way to do output escaping using xpath? I would like to generate a file with urls in quotes like this <http://eg.com/hello> I suppose one can set text output mode, but as I would also like to copy chunks of html into the result document text output is not an option. Or at least it was not an option using xslt.


Replies (10)

Please register to reply

RE: output escaping in xpath? - Added by Anonymous almost 18 years ago

Legacy ID: #3780145 Legacy Poster: Michael Kay (mhkay)

XPath doesn't do output, so I'm not sure exactly what you mean. Do you mean, can you do serialization using a Saxon API used in conjunction with the XPath API? The answer to that is yes, though it's always hard to find the nuggets you need in the Saxon API. If you want to serialize a tree, you cau use the method net.sf.saxon.Query.serialize(), which allows you full control over the output properties. If you want to write events to a serializer, try something like: PipelineConfiguration = config.makePipelineConfiguration(); Receiver receiver = SerializerFactory.getReceiver(result, pipe, props); ReceivingContentHander ch = new ReceivingContentHander(); ch.setReceiver(receiver); ch.setPipelineConfiguration(pipe); and then you can write SAX events to ch.

RE: output escaping in xquery? - Added by Anonymous almost 18 years ago

Legacy ID: #3780173 Legacy Poster: Henry Story (bblfish)

Oh silly me, I confused xpath and xquery. Let me be clearer anyway. You can use Saxon on the command line and output a file like so hjs@bblfish:0$ saxonq -s entry.xml atom2turtle.xquery <?xml version="1.0" encoding="UTF-8"?>@prefix a: &lt;hello&gt; . [ a :Entry (where saxonq is just a shell script calling java, setting the jar file, and passing the XQuery class name) What I would like is for the &lt;hello&gt; to be output as <hello>. With xslt you can ask to disable output escaping using <xsl:text disable-output-escaping="yes"> @prefix : &lt;https://sommer.dev.java.net/atom/2006-06-06/ </xsl:text> Or you can set text output mode using <xsl:output method="text"> but if you do this then you can use <xsl:copyof...> to copy whole blocks of xml with their xml annotations out, which I would also like. Anyway, I was wondering if there was an equivalent to the disable-output-escaping in XQuery

RE: output escaping in xquery? - Added by Anonymous almost 18 years ago

Legacy ID: #3780237 Legacy Poster: Michael Kay (mhkay)

No, there's no equivalent to disable-output-escaping in XQuery. (Which is probably a good thing, since the facility is so widely abused in XSLT.) What are you actually trying to achieve: why do you think you need d-o-e? Michael Kay

RE: output escaping in xquery? - Added by Anonymous almost 18 years ago

Legacy ID: #3780259 Legacy Poster: Henry Story (bblfish)

I have written a transform from the Atom feed format to an rdf ontology written out in N3, because N3 is just a heck of a lot more readable that rdf/xml. It is available under a BSD licence here btw: https://sommer.dev.java.net/atom/ If you look at the examples you will see why I need both access to parts of the xml (they go into the content) and why I need to be able to d-o-e on <>. Would you say this is a misuses of d-o-e? It seems to me that this is a really good case for them. Anyway, I was thinking of writing the same out in XQuery just for fun.

RE: output escaping in xquery? - Added by Anonymous almost 18 years ago

Legacy ID: #3780353 Legacy Poster: Michael Kay (mhkay)

You appear to be generating a text file that contains XML fragments embedded within it. I think the right approach to this is to use the text output method, and to use saxon:serialize() to construct the XML fragments.

RE: output escaping in xquery? - Added by Anonymous almost 18 years ago

Legacy ID: #3780729 Legacy Poster: Henry Story (bblfish)

Thanks for the tip. It is a pitty that this method is not in the standard as it seems really quite useful. I still have a problem. The following xquery declare namespace a = "http://www.w3.org/2005/Atom"; declare namespace saxon = "http://saxon.sf.net/"; declare option saxon:output "method=text"; declare option saxon:output "omit-xml-declaration=yes"; <text> hello { for $link in //a:link return saxon:serialize($link,"xml") } </text> outputs the following hjs@bblfish:0$ saxonq -s entry.xml atom2turtle.xquery hello <?xml version="1.0" encoding="UTF-8"?><link xmlns="http://www.w3.org/2005/Atom" rel="alternate" type="text/html" href="http://example.org/2005/04/02/atom&quot;/> <?xml version="1.0" encoding="UTF-8"?><link xmlns="http://www.w3.org/2005/Atom" rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3&quot;/> As you can see every call to the serialize() method adds an xml declaration. Is there a way to stop this? It does not seem to pick up the option setting above.

RE: output escaping in xquery? - Added by Anonymous almost 18 years ago

Legacy ID: #3780735 Legacy Poster: Michael Kay (mhkay)

In XSLT saxon:serialize can refer to a named output declaration but there's no equivalent in XQuery. Sorry! Something for the TODO list.

RE: output escaping in xquery? - Added by Anonymous almost 18 years ago

Legacy ID: #3780770 Legacy Poster: Henry Story (bblfish)

Oh that is a pitty. I was thinking of re-implementing my transform with XQuery, because it just seemed like it would be a lot easier to generate nicely formatted N3 with XQuery. Well I look forward to having an improoved serialize method. It should not be to difficult to implement I imagine, no? I am not sure if Xquery allows for a vararg number of arguments then one could have a method such as saxon:serialise(.,"xml","omit-xml-declaration=yes"); Thanks for the help in the meantime. Henry

RE: output escaping in xquery? - Added by Anonymous almost 18 years ago

Legacy ID: #3780981 Legacy Poster: Henry Story (bblfish)

Ok I found a work around: declare function loc:serialize($el as element()) as xs:string { replace(saxon:serialize($el,"xml"),"^[^&gt;]*>","") }; That will remove the initial declaration. Still it would be good to be able to avoid having to do that :-) Thanks again Henry

RE: output escaping in xpath? - Added by Anonymous almost 18 years ago

Legacy ID: #3782499 Legacy Poster: Michael Kay (mhkay)

This enhancement to saxon:serialize will appear in the next release. I decided to do it by allowing the second argument to be an xsl:output element, for example in XQuery you will be able to write: declare namespace saxon="http://saxon.sf.net/"; declare namespace xsl="http://www.w3.org/1999/XSL/Transform"; <out>{ let $x := <a><b/><c>content</c><?pi?><!--comment--></a> return saxon:serialize($x, <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" saxon:indent-spaces="1"/>) }</out>

    (1-10/10)

    Please register to reply