How Do I Get All Decendents in XPath/XQuery?
I am trying to navigate a document to learn about its structure. The document is being served to me so I don't have access to the raw document, but I can exercise queries against the server. I believe it is schema-less. I am accessing the document through the CQ web application which is part of MarkLogic.
I would bas开发者_C百科ically like to get a fully populated tree returned to me. This seems really easy, but has not proven to be. I've looked through the W3C and a couple other sites and nothing seems to work.
Thanks-in-advance,
Guido
Maybe the document is too big to return - if you're using MarkLogic, maybe you're trying to query a "forest" of thousands or millions of subdocuments?
A good way to learn about the structure of a document without trying to return all of it would be to use successive XPath queries that give you the names of elements. E.g.
name(/*)
This will tell you the name of the outermost element. Then,
name(/*/*[1]) <!-- name of first child of outermost element -->
name(/*/*[2])
/*/text()[1] <!-- content of first text node under outermost element -->
count(/*/*) <!-- number of children of outermost element -->
name(/*/@*[1]) <!-- name of first attribute of outermost element (untested) -->
etc.
Since you can use XQuery, you could do a loop that prints out, say, all the above data for the first three elements at the top three levels of the document.
Alternatively, /
may return nothing, because in XPath this means "the root node of the document containing the context node"; and in XQuerying a database of XML documents, there may not yet be a context node (caveat: I'm not real fluent in XQuery, so check your references). Instead, you may have to start your XPath expression with document('...')/
; hopefully you know the name of a document?
Also, this screenshot shows some potentially useful queries. I think.
@LarsH recommended a useful exploration strategy.
An alternative is to get the whole XML document, for example applying the XSLT identity transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This transformation produces as result an XML document that in most cases is identical to the source XML document (any XML document) on which it is applied.
Another way of seeing the exact XML document is to use a debugger and set a breakpoint at a place in the code where the XML document has already been received. Then use the debugger visualization capabilitis to get the "outerxml" or "innerxml" property of the XMLDocument object.
Of course, nothing prevents the server of returning different XML documents on different requests.
Since you are using CQ, you can click the "explore" link (towards the upper left of the query pane). This will give you a list of documents in the database you have selected. You can then use the URI of one of the documents and do an fn:doc of it:
fn:doc("/myuri.xml")
That will return that one document. Then you can add XPath steps to navigate down it.
精彩评论