正则表达式修改xpath表达式

编程入门 行业动态 更新时间:2024-10-14 00:29:13
本文介绍了正则表达式修改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表达式

本文发布于:2023-11-12 10:31:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   正则表达式   xpath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!