5

XML ファイルのクエリに BaseX ネイティブ XML データベースを使用しています。BaseX ドキュメントで提供されている BaseXClient.java ファイルを使用しています。basex サーバーを起動し、BaseXClient.java を使用してサーバーに接続しています。

// create session
final BaseXClient session = new BaseXClient("localhost", 1984, "admin", "admin");

String query = "doc('xmlfiles/juicers.xml')//image";
// version 1: perform command and print returned string
System.out.println(session.execute(query));

これで、juicers.xml ファイルにxmlns情報が含まれるようになりました。

<?xml version="1.0"?>
<juicers
xmlns="http://www.juicers.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.juicers.org 
                    juicers.xsd">

<juicer>
    <name>OJ Home Juicer</name>
    <image>images\mighty_oj.gif</image>
    <description>There&apos;s just no substitute for a properly squeezed
        orange in the morning. So delicate and refreshing. The finest hotels
        use mechanical juicers of this type for their most discriminating
        guests. This is the largest selling juicer of its kind. It&apos;s a
        beautiful little all-metal piece in baked enamel and polished chrome;
        it even won the Frankfurt Fair Award for its design. Uses no
        electricity and produces no non-recyclable waste as do frozen juices.
    </description>
    <warranty>lifetime warranty</warranty>
    <cost>41.95</cost>
    <retailer>http://www.thewhitewhale.com/oj.htm</retailer>
</juicer>
</juicers>

xmlnsXML インスタンス ファイル (juicers.xml) を指定しないと、正しい結果が返されます。ただしxmlns、XML インスタンス ファイルに含まれている場合は、次の例外がスローされます。

java.io.IOException: Stopped at line 1, column 3:
Expecting command.
at org.basex.api.BaseXClient.execute(BaseXClient.java:73)
at org.basex.api.BaseXClient.execute(BaseXClient.java:84)
at org.basex.api.Example.main(Example.java:31)

を使用して XML インスタンス ファイルをクエリするにはどうすればよいxmlnsですか? 抜け道はありますか?xqueryJavaから実行する他の方法はありますか?

4

2 に答える 2

3

Chrstian の回答に加えて、要素をアドレス指定するたびに、デフォルトの要素の名前空間を宣言するか、名前空間を使用する必要があります (ドキュメントに複数の名前空間がある場合は、おそらくこれを行う必要があります)。

デフォルトの要素名前空間を使用すると、上記のようにクエリを記述できます。

declare default element namespace "http://www.juicers.org";
doc('xmlfiles/juicers.xml')//image

ジューサーをデフォルトの要素名前空間として使用したくない場合は、それを名前空間として宣言し、要素レベルで参照します。

declare namespace juicers="http://www.juicers.org";
doc('xmlfiles/juicers.xml')//juicers:image

名前空間識別子はjuicers任意に設定できます。

于 2012-04-01T11:54:40.437 に答える
1

XQUERY次のコマンドをクエリの前に付ける必要があります。

System.out.println(session.execute("XQUERY " + query));

別のオプションは、「クエリ」インスタンスを作成し、query.execute()その後呼び出すことです。

BaseXClient.Query query = session.query(query);
System.out.println(query.execute())
于 2012-03-31T20:26:48.713 に答える