简体   繁体   中英

How do you handle complex namespaces in XQuery/BaseX?

I have a relatively complex XML file following the ReqIF standard that I wish to manipulate using BaseX. When I strip out all of the xlmns attributes in the REQ-IF node, then I can perform XQueries and traverse the file (eg, //REQ-IF/THE-HEADER ) in the XQuery bar, and see the results as expected.

However when I include the xlmns attributes, none of the queries work. Even worse, when I select a node in the Map View to copy the path and paste it into the XQuery bar, no results are returned either.

So, how does one express an XQuery (using presumably the namespace information) to get to the innards of this XML file?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd reqif.xsd" xmlns:reqif="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd" xmlns:reqif-xhtml="http://www.w3.org/1999/xhtml" xmlns:rm-reqif="http://www.ibm.com/rm/reqif" xmlns:xhtml="http://www.w3.org/1999/xhtml" >
    <THE-HEADER>
    </THE-HEADER>
    <CORE-CONTENT>
        <REQ-IF-CONTENT>
            <DATATYPES>             
            </DATATYPES>
            <SPEC-TYPES>
            </SPEC-TYPES>
            <SPEC-OBJECTS>
            </SPEC-OBJECTS>
            <SPEC-RELATIONS>
            </SPEC-RELATIONS>
            <SPECIFICATIONS>
            </SPECIFICATIONS>
            <SPEC-RELATION-GROUPS>
            </SPEC-RELATION-GROUPS>
        </REQ-IF-CONTENT>
    </CORE-CONTENT>
</REQ-IF>

Well, look into your favourite XQuery tutorial or in the spec, it shows you can declare a default element namespace https://www.w3.org/TR/xquery-31/#id-default-namespace

declare default element namespace "http://www.omg.org/spec/ReqIF/20110401/reqif.xsd";

/REQ-IF/CORE-CONTENT/REQ-IF-CONTENT/DATATYPES

(example https://xqueryfiddle.liberty-development.net/pPgCcoC )

or another namespace https://www.w3.org/TR/xquery-31/#id-namespace-declaration

declare  namespace reqif = "http://www.omg.org/spec/ReqIF/20110401/reqif.xsd";

/reqif:REQ-IF/reqif:CORE-CONTENT/reqif:REQ-IF-CONTENT/reqif:DATATYPES

(online at https://xqueryfiddle.liberty-development.net/pPgCcoC/1 )

In addition there wild card namespace expressions like /*:REQ-IF and enhanced QNames like ( /Q{http://www.omg.org/spec/ReqIF/20110401/reqif.xsd}REQ-IF )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM