Return entire XML document & doc-uri with embedded triples using Sparql

Return entire XML document & doc-uri with embedded triples using Sparql

xmldocument
xmldocument javascript
javascript parse xml string
how to retrieve data from xml file using javascript
xpath
xml dom
getelementsbytagname
domparser

I have some XML documents with embedded triples.

I can run a sem:sparql query with no issues, but I'm not sure how to return the entire XML document that the embedded triples are in along with the document URI with the results. Thanks in advance

Does anyone know how to do this ?

xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics"  at "/MarkLogic/semantics.xqy";

let $res := sem:sparql('
  SELECT ?country
  WHERE {
    <http://example.org/news/Nixon> <http://example.org/wentTo> ?country
  }
  ',
  (),
  (),
  cts:and-query( (

    cts:or-query( (
      cts:element-value-query( xs:QName("source"), "AP Newswire" ),
      cts:element-value-query( xs:QName("source"), "BBC" )
   ) )
 ) )
)


return $res;

There may be a cleaner way (with less casting) but this is how I have done it before in XQuery :

xquery version "1.0-ml"; 

import module namespace sem = "http://marklogic.com/semantics" 
      at "/MarkLogic/semantics.xqy";

let $triples := sem:sparql('
SELECT *
WHERE
{ ?subject ?predicate ?object }
')

return sem:database-nodes($triples ! sem:triple(map:get(., "subject"), map:get(., "predicate"), map:get(., "object"))) ! fn:base-uri(.)

XML DOM - Document object, The Document object represents the entire XML document. for the document. doctype, Returns the Document Type Declaration associated with the document. I have tried and failed to find out how to get the entire XML string from the XMLDocument returned by a GET. There are a lot of questions on SO on how to find or replace specific elements in the ob


I solved this myself by doing the it this way. I hope this helps someone else. It took me a solid 5+ hours of reading through MarkLogic documentation. I think I have a good handle on it all now. I'm not sure this is the fastest way, but it only took 93ms

for $id in  sem:query-results-serialize(  $rez  , 'xml')//s:uri/text()
          return   
  xdmp:node-uri( (cts:search(/blah//sem:triple/*[text() = $id ]  , ()))[1]    ) 

Parsing and serializing XML, Constructs a DOM tree by parsing a string containing XML, returning a XMLDocument or Document as appropriate  Pieces of or entire XML documents can be queried and retrieved using these methods. Queries can return fragments or entire XML documents, and results returned from queries can be limited by using predicates. Because queries on XML data return XML sequences, a query's result can be used in the construction of XML data as well.


Consider using cts:triple-range-query. It can be used in cts:search directly, and blended into your other query. It allows looking for individual triples only, though your example is fairly simple.

To find all AP Newswire and BBC reports talking about Nixon visiting countries:

query version "1.0-ml";

for $doc in cts:search(
  collection(),
  cts:and-query( (
    cts:element-value-query( xs:QName("source"), ("AP Newswire", "BBC") ),
    cts:triple-range-query(
      sem:iri("http://example.org/news/Nixon"),
      sem:iri("http://example.org/wentTo"),
      ()
    )
  ) )
)[1 to 10]
return (xdmp:node-uri($doc), $doc)

You can also pre-execute a SPARQL to resolve more complex SPARQL queries first, and feed the result into a cts:search with a triple-range-query. For example: to find all reports about specific countries visited by Nixon (as reported by AP Newswire and BBC):

let $countries := sem:sparql('
  SELECT DISTINCT ?country
  WHERE {
    <http://example.org/news/Nixon> <http://example.org/wentTo> ?country
  }
  ',
  (),
  (),
  cts:element-value-query( xs:QName("source"), ("AP Newswire", "BBC") )
) ! map:get(., "country")

for $doc in cts:search(
  collection(),
  cts:triple-range-query(
    sem:iri("http://example.org/news/Nixon"),
    sem:iri("http://example.org/wentTo"),
    $country[1 to 3]
  )
)[1 to 10]
return (xdmp:node-uri($doc), $doc)

Note the subtle differences between the above two examples..

HTH!

Modifying Nodes, Content, and Values in an XML Document , Modifying Nodes, Content, and Values in an XML Document. 03/30/ Modify an entire set of nodes by replacing the nodes with new nodes. When using the ReplaceData and RemoveChild methods, the methods return the  The solution I found was to get the org.w3c.dom.Node with xpath (DOM would work too). Then I created a javax.xml.transform.dom.DOMSource from the node and transformed that to a string with javax.xml.transform.TransformerFactory.


XmlDocument.GetElementsByTagName Method (System.Xml , The special value "*" matches all tags. Returns. XmlNodeList. An XmlNodeList containing a list of all matching nodes. If no nodes match name , the returned  The Document object represents the entire XML document. The Document object is the root of an XML document tree, and gives us the primary access to the document's data. Since element nodes, text nodes, comments, processing instructions, etc. cannot exist outside the document, the Document object also contains methods to create these objects.


Returning an XML Encoded String in .NET, However, in this case I have a huge block of mostly static XML text and creating the entire document using structured XML documents seems  XML is a markup language created by the World Wide Web Consortium (W3C) to define a syntax for encoding documents that both humans and machines could read. It does this through the use of tags that define the structure of the document, as well as how the document should be stored and transported. It’s probably easiest to compare it to another


Read XML document and return Document Object Model node , This MATLAB function reads the specified XML file and returns DOMnode a Document Object Model node representing the document. The Select-Xml cmdlet lets you use XPath queries to search for text in XML strings and documents. Enter an XPath query, and use the Content, Path, or Xml parameter to specify the XML to be searched.