XSLT:无法按名称选择正确的祖先

时间:2018-05-01 04:12:04

标签: xml xslt

尝试在XML文件中选择祖先节点时,我会遇到令人困惑的行为。 XML看起来像这样:

private static void Register() throws FileNotFoundException{
        try{            

            FileWriter fw = new FileWriter("info.txt", true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter out = new PrintWriter(bw);           


            Scanner scan = new Scanner(System.in);
            System.out.println("Enter a username: ");
            String username = scan.nextLine();
            System.out.println("Enter a password: ");
            String password = scan.nextLine();
            out.println(username + " " + password);

            out.flush();


        }catch(IOException e){
            e.printStackTrace();
        }
    }

因此,在XSLT中,我在与morph / item节点匹配的模板中,我尝试使用<?xml version="1.0" encoding="utf-8"?> <document version="2"> <interlinear-text> <paragraphs> <paragraph> <phrases> <phrase> <item type="segnum" lang="en">1.1</item> <words> <word> <morphemes> <morph type="stem"> <item type="cf" lang="tmy-Latn">yau</item> <item type="txt" lang="tmy-Latn">yau</item> <item type="gls" lang="en">1S</item> </morph> </morphemes> </word> <word> <morphemes> <morph type="stem"> <item type="cf" lang="tmy-Latn">ma</item> <item type="txt" lang="tmy-Latn">ma</item> <item type="gls" lang="en">and</item> </morph> </morphemes> </word> ... ,而是返回ancestor::*[word]元素。即使我说<words>。为什么选择与我要求的元素不匹配的元素?!如何使用祖先函数从item元素中按名称选择单词元素?

1 个答案:

答案 0 :(得分:0)

您可以使用以下xpath查找尊重约束项[1]文本等于<word>的所有ma个节点,您可以根据需要调整提取条件。

<强> XPATH:

//morphemes/morph/item[1][text()='ma']/ancestor::word

<强>样本:

(xmllint --xpath "//morphemes/morph/item[1][text()='ma']/ancestor::word" test_ancestors.xml; echo) | xmllint --format -
<?xml version="1.0"?>
<word>
  <morphemes>
    <morph type="stem">
      <item type="cf" lang="tmy-Latn">ma</item>
      <item type="txt" lang="tmy-Latn">ma</item>
      <item type="gls" lang="en">and</item>
    </morph>
  </morphemes>
</word>

如果这有助于您,请告诉我。