2

私は Java で XPath 式を評価していましたが、返されるString値は 1 つだけであるはずでした。
ただし、取得するのは空白の値だけです。

SSCCE

import javax.xml.xpath.*;
import java.io.*;
import org.xml.sax.*;

public class XPathBasic{
    public static void main(String[] args){
        XPathFactory factory;
        XPath xPath;
        XPathExpression xPathExpressionCompiled;
        String xPathExpressionString;
        File xmlFile;
        InputSource inputSource;

        try{
            factory = XPathFactory.newInstance();
            xPath = factory.newXPath();
            xPathExpressionString = "/people/student[@scholarship='yes']/name";
            xPathExpressionCompiled = xPath.compile(xPathExpressionString);

            xmlFile = new File("helloWorld.xml");
            inputSource = new InputSource(new FileInputStream(xmlFile));
            String name = xPathExpressionCompiled.evaluate(inputSource);

            if(name != null){
                System.out.println("Name of the student is: " + name);
            }else{
                System.out.println("Error");
            }
        }catch(XPathExpressionException e){
            System.out.println("There seems to be an error with your expression: " + e.getMessage());
        }catch(IOException e){

        }catch(Exception e){

        }

    }
}  

XML

<?xml version="1.0" encoding="UTF-8" ?>
<people xmlns="http://www.cmu.edu/ns/blank" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd">
    <student scholarship="yes">
        <name>John</name>
        <course>Computer Technology</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>

    <student scholarship="no">
        <name>Foo</name>
        <course>Industrial Electronics</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>
</people>  

出力

Name of student is:

そこでジョンに会えると思っていた。誰かが何が悪かったのか教えてもらえますか?

4

2 に答える 2

2

これは私たちの多くが犯す間違いです。Xpath は名前空間に敏感です。XML ファイルには名前空間付きの要素が含まれており、これらの名前空間は XPath で必要です。XML ファイルではデフォルトの名前空間を使用できますが、XPath では使用できません。

試す

"/people[namespace-uri()='http://www.cmu.edu/ns/blank']/student[namespace-uri()='http://www.cmu.edu/ns/blank' and @scholarship='yes']/name[namespace-uri()='http://www.cmu.edu/ns/blank']"

そしてそれはうまくいくはずです。

お使いのシステムでは、おそらく XPath で名前空間プレフィックスを使用できます。これらを空にすることはできません。例は次のとおりです。

"/a:people/a:student[@scholarship='yes']/a:name"

名前空間プレフィックス (a) をどのようにマッピングするかは、ソフトウェアによって異なります。マッピングされている限り、どのプレフィックスでも機能することに注意してください。

于 2013-05-13T20:39:22.887 に答える
1

私は Jdom2 で Jaxen を使用しましたが、これが役立つことがわかりました: http://www.edankert.com/defaultnamespaces.html

これを機能させる 1 つの方法は、デフォルトの名前空間のプレフィックスを定義することです。ここではデフォルトと名付けました。(プレフィックスには任意の名前を付けることができます。) 次に、xpath で新しいプレフィックスを参照します。

Namespace nameSpaceDefault = 
    Namespace.getNamespace("default", "http://www.cmu.edu/ns/blank");
Namespace nameSpaceXsi = 
    Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

nameSpaces.add(nameSpaceDefault);
nameSpaces.add(nameSpaceXsi);

String xpath = "/default:people/default:student[@scholarship='yes']/default:name/text()";

XPathExpression<Text> xpathExpression = XPathFactory.instance().
            compile(xpath, Filters.text(), null, nameSpaces);

//Jdom2 document
Document document = ...;  

//Evaluate xpath
List<Text> textValues = xpath.evaluate(document);  
于 2014-06-20T15:23:59.780 に答える