通过XPath表达式提取一个以上属性节点值的值

如何通过XPath表达式提取多个属性节点的值?

一个XML文件的例子是。

<tag1>
    <tag2>
        <tag3 id="1">
            <tage4>
                <tage4code code="1">
                    <tag5>
                        <tage4Value Day="14" Month="Oct" Year="2000" />
                    </tag5>
                    <tag5>
                        <tage4Value Month="Oct" Year="2001" />
                    </tag5>
                    <tag5>
                        <tage4Value Year="2002" />
                    </tag5>
                    <tag5>
                        <tage4Value Day="1" Month="Jan" Year="1999" />
                    </tag5>
                    <tag5>
                        <tage4Value Year="1940" />
                    </tag5>
                </tage4code>
            </tage4>
        </tag3>
    </tag2>
</tag1>

到目前为止,我有这个XPath字符串。

XPathExpression expr = xpath.compile("concat((/tag1/tag2/tag3[@id=1]/tage4/tage4code[@code=1]/tag5/tage4Value/@Day, '/' , /tag1/tag2/tag3[@id=1]/tage4/tage4code[@code=1]/tag5/tage4Value/@Month, '/', /tag1/tag2/tag3[@id=1]/tage4/tage4code[@code=1]/tag5/tage4Value/@Year)");
                     NodeList combination1 = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
                     for (int a = 0; a <= combination1.getLength(); a++) {
                            System.out.println("date : " + combination.item(a).getNodeValue());
                        }

我的预期结果

14Oct2000Oct200120021Jan19991940

谁能帮我纠正一下我的XPathExpression?

解决方案:

XPath 2.0解决方案 :

tokenize(replace(replace(replace(substring-after(string-join(//tag5/*|//tag5//@*,","),","),",,","%"),","," ")," ","/"),"%")

输出。

String='14/Oct/2000'
String='Oct/2001'
String='2002'
String='1/Jan/1999'
String='1940'

XPath 1.0解决方案 :

concat(translate(normalize-space(concat((//tage4Value)[1]/@Day," ",(//tage4Value)[1]/@Month," ",(//tage4Value)[1]/@Year))," ","/"),"|",translate(normalize-space(concat((//tage4Value)[2]/@Day," ",(//tage4Value)[2]/@Month," ",(//tage4Value)[2]/@Year))," ","/"),"|",translate(normalize-space(concat((//tage4Value)[3]/@Day," ",(//tage4Value)[3]/@Month," ",(//tage4Value)[3]/@Year))," ","/"),"|",translate(normalize-space(concat((//tage4Value)[4]/@Day," ",(//tage4Value)[4]/@Month," ",(//tage4Value)[4]/@Year))," ","/"),"|",translate(normalize-space(concat((//tage4Value)[5]/@Day," ",(//tage4Value)[5]/@Month," ",(//tage4Value)[5]/@Year))," ","/"))

輸出:

String='14/Oct/2000|Oct/2001|2002|1/Jan/1999|1940'

或用新的分线器。

concat(translate(normalize-space(concat((//tage4Value)[1]/@Day," ",(//tage4Value)[1]/@Month," ",(//tage4Value)[1]/@Year))," ","/"),codepoints-to-string(10),translate(normalize-space(concat((//tage4Value)[2]/@Day," ",(//tage4Value)[2]/@Month," ",(//tage4Value)[2]/@Year))," ","/"),codepoints-to-string(10),translate(normalize-space(concat((//tage4Value)[3]/@Day," ",(//tage4Value)[3]/@Month," ",(//tage4Value)[3]/@Year))," ","/"),codepoints-to-string(10),translate(normalize-space(concat((//tage4Value)[4]/@Day," ",(//tage4Value)[4]/@Month," ",(//tage4Value)[4]/@Year))," ","/"),codepoints-to-string(10),translate(normalize-space(concat((//tage4Value)[5]/@Day," ",(//tage4Value)[5]/@Month," ",(//tage4Value)[5]/@Year))," ","/"))

輸出。

String='14/Oct/2000
Oct/2001
2002
1/Jan/1999
1940'

给TA打赏
共{{data.count}}人
人已打赏
未分类

Angular Nebular样式不适用于NbChatComponent。

2022-9-9 2:56:24

未分类

使用给定的模式在Postgres中生成列。

2022-9-9 3:07:17

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索