正则表达式修改xpath表达式 [英] Regex to modify xpath expression

查看:167
本文介绍了正则表达式修改xpath表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用我正在使用DOMParser的Java代码来验证Xpath表达式。我面临的问题是,对于默认命名空间,我需要调整xpath,然后才能通过代码进行评估。对于例如如果我想使用以下xpath -

I have to verify Xpath expressions using Java code for which I am using the DOMParser. The problem that I am facing is that for default namespaces I need to tweak the xpath before it can be evaluated by the code. For e.g. if I want to use the following xpath -

// party [@ id ='party1:abc'] / abc:person [@id ='交易者'] /交易/ abc:personId [@ personIdScheme ='urn:xyz:person-id:PEOPLESOFT'] / text()

我需要将此代码传递给代码 -

I need to pass this to code as -

//:party [@ id ='party1:abc'] / abc: person [@ id ='Trader'] /:trade / abc:personId [@ personIdScheme ='urn:xyz:person-id:PEOPLESOFT'] / text()

所以基本上要求是在所有相关节点之前放置一个

So basically the requirement is to put a : before all relevant nodes where default namespace is applicable.

有人可以帮助一个可以有效地进行这种转换的正则表达式吗?

Can someone please help with a regex which can do this conversion efficiently?

推荐答案

String str = "//party[@id='party1:abc']/abc:person[@id='Trader']/trade/abc:personId[@personIdScheme='urn:xyz:person-id:PEOPLESOFT']/text()";
str = str.replaceAll("/(?=\\w.*?/)(?![^\\[/]*:)","/:");

正则表达式查找的是 / 不是最后一个,后跟一个单词字符。

它还忽略那些已经之前的 [ /

The regex looks for the / that are not the last one, and are followed by a word character.
And it also ignores those that already have a : before the [ or /.

这篇关于正则表达式修改xpath表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆