基于动态正则表达式的XPath搜索 [英] XPath search based on dynamic regular expressions

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

问题描述

我有一个类似下面的XML:

I have an XML like the one below:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration>
        <Destinations>
            <Destination name="DEST1" >
                <From>AMA*</From>
            </Destination>

            <Destination name="DEST2" >
                <From>AMAZON</From>
            </Destination>

                           <Destination name="DEST3" >
                <From>EBAY</From>
            </Destination>

                           <Destination name="DEST4" >
                <From>*</From>
            </Destination>

        </Destinations>
    </Configuration>

我想查询并查找与提供的输入相匹配的所有设计。

I want to query and find out all desintations that match a provided input.

如果我指定EBAY,我希望xpath返回节点名称= DEST3和DEST4(它不关心该值)但如果我指定AMAZON则需要返回DEST1,DEST2和DEST4因为DEST1中的AMA *支持通配符。

If i specify EBAY i want the xpath to return the node name = DEST3 and DEST4 (which doesn't care about the value) but if i specify AMAZON it needs to return DEST1, DEST2 and DEST4 as AMA* in DEST1 supports wild card character.

到目前为止,我的XPath看起来像这样:

so far my XPath looks like this:


/目的地[(From =' '或
From ='*'))] / @ name

/Destination[(From = '' or From = '*' ) )]/@name

如果指定了输入,我会动态创建XPATH表达式并将输入值插入 字段

If input is specified i create the XPATH expressions dynamically and slot the incoming value in the field

I除了From之外,我的XML中还有其他元素。

I have other elements in my XML besides From.

感谢是否有人可以指点这一点。

Appreciate if anyone could give pointers on this.

谢谢,
Manglu

Thanks, Manglu

推荐答案

以下XPath 2.0表达式表达了所需的选择

  /*/*/*[From[matches($pPat, replace(., '\*', '.*'))]]

解释


  1. $ pPat 变量包含搜索模式(例如'EBAY', 'AMAZON'等。)

  1. The $pPat variable contains the search pattern (such as 'EBAY', 'AMAZON', etc.).

标准XPath 2.0函数 matches()用于将任何 From 元素的值与字符串模式匹配。

The standard XPath 2.0 function matches() is used to match the value of any From element to the string pattern.

任何<$ c的值$ c>从元素转换为XPath 2.0支持的标准正则表达式。为此目的,任何'*'的出现(以\ *的形式转义,以免被带走正如字符串。*

The value of any From element is converted to a standard regular expression as supported by XPath 2.0. For this purpose, any occurence of '*' (escaped as "\*" in order not to be taken as the special char '*' used in regexs but as a normal character) is replaced by the string ".*"

测试

我使用了以下XSLT 2.0转换并验证上面的XPath 2.0表达式按预期选择了元素。要使用它,请将全局参数$ pPat的值替换为任何所需的值。

I have used the following XSLT 2.0 transformation and verified that the above XPath 2.0 expression selects the elements as expected. To use it, replace the value of the global parameter $pPat with any desired value.

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes"/>

    <xsl:param name="pPat" as="xs:string"
     select="'AMAZON'"/>

    <xsl:variable name="vsrchResult" as="element()*"
     select="/*/*/*[From[matches($pPat, replace(., '\*', '.*'))]]"/>

    <xsl:template match="/">
      <xsl:copy-of select="$vsrchResult"/>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于最初提供的XML文档时

<Configuration>
    <Destinations>
        <Destination name="DEST1" >
            <From>AMA*</From>
        </Destination>
        <Destination name="DEST2" >
            <From>AMAZON</From>
        </Destination>
        <Destination name="DEST3" >
            <From>EBAY</From>
        </Destination>
        <Destination name="DEST4" >
            <From>*</From>
        </Destination>
    </Destinations>
</Configuration>

生成所需的输出

<Destination name="DEST1">
            <From>AMA*</From>
        </Destination><Destination name="DEST2">
            <From>AMAZON</From>
        </Destination><Destination name="DEST4">
            <From>*</From>
        </Destination>

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

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