这个 XPath 表达式可以缩小吗

Can this XPath expression be minified

我不是 XPath 专家,我找不到执行此操作的工具。有没有办法缩小这个 XPath 语句? 80% 是多余的。

XPATH 2

 (//CustomField | //fields)[
     (fullName/text[not(matches(@Image, "chk_[a-zA-Z0-9]+__c"))] and type/text[@Image="Checkbox"]) or
     (fullName/text[not(matches(@Image, "txt_[a-zA-Z0-9]+__c"))] and type/text[@Image="Text"]) or
     (fullName/text[not(matches(@Image, "txa_[a-zA-Z0-9]+__c"))] and type/text[@Image="Textarea"]) or
     (fullName/text[not(matches(@Image, "txr_[a-zA-Z0-9]+__c"))] and type/text[@Image="Richtext"]) or
     (fullName/text[not(matches(@Image, "txl_[a-zA-Z0-9]+__c"))] and type/text[@Image="LongTextArea"]) or
     (fullName/text[not(matches(@Image, "num_[a-zA-Z0-9]+__c"))] and type/text[@Image="Number"]) or
     (fullName/text[not(matches(@Image, "dat_[a-zA-Z0-9]+__c"))] and type/text[@Image="Date"]) or
     (fullName/text[not(matches(@Image, "lkp_[a-zA-Z0-9]+__c"))] and type/text[@Image="Lookup"]) or
     (fullName/text[not(matches(@Image, "mdr_[a-zA-Z0-9]+__c"))] and type/text[@Image="MasterDetail"]) or
     (fullName/text[not(matches(@Image, "dtm_[a-zA-Z0-9]+__c"))] and type/text[@Image="DateTime"]) or
     (fullName/text[not(matches(@Image, "url_[a-zA-Z0-9]+__c"))] and type/text[@Image="Url"]) or
     (fullName/text[not(matches(@Image, "pkl_[a-zA-Z0-9]+__c"))] and type/text[@Image="Picklist"]) or
     (fullName/text[not(matches(@Image, "pkm_[a-zA-Z0-9]+__c"))] and type/text[@Image="MultiselectPicklist"]) or
     (fullName/text[not(matches(@Image, "cur_[a-zA-Z0-9]+__c"))] and type/text[@Image="Currency"]) or
     (fullName/text[not(matches(@Image, "pct_[a-zA-Z0-9]+__c"))] and type/text[@Image="Percent"]) or
     (fullName/text[not(matches(@Image, "fcur_[a-zA-Z0-9]+__c"))] and type/text[@Image="Currency"] and formula) or
     (fullName/text[not(matches(@Image, "rcnt_[a-zA-Z0-9]+__c"))] and type/text[@Image="Summary"] and summaryOperation/text[@Image="count"]) 
]

这是 XML 文档的精简版,只有一个匹配项

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">

    <fields>
        <fullName>txl_Description__c</fullName>
        <deprecated>false</deprecated>
        <externalId>false</externalId>
        <label>Price Schedule Description</label>
        <length>4000</length>
        <trackHistory>false</trackHistory>
        <trackTrending>false</trackTrending>
        <type>LongTextArea</type>
        <visibleLines>5</visibleLines>
    </fields>
    <fields>
        <fullName>Reason_For_Override__c</fullName>
        <deprecated>false</deprecated>
        <externalId>false</externalId>
        <label>Reason for Override</label>
        <length>1000</length>
        <trackHistory>false</trackHistory>
        <trackTrending>false</trackTrending>
        <type>LongTextArea</type>
        <visibleLines>2</visibleLines>
    </fields>
</CustomObject>

我仔细检查了你的表达,看起来冗余度接近于零。 _[a-zA-Z0-9]+__c 部分与优化表达式的相关性较低。

创建像 ("chk","Checkbox")("txt","TextBox") 这样的元组既不会提高可读性也不会提高性能,即使看起来是这样。所以你很高兴,AFAIK。

您可以将所有出现的 text() 替换为 .,无论如何这是更好的做法。

您可以传入包含查找 table 的参数 $data<data><key a="chk" b="Checkbox"/><key a="..." b="..."/>...</data> 然后执行

some $key in $data/data/key 
satisfies fullName[not(matches(., concat($key/@a, "_[a-zA-Z0-9]+__c"))] 
          and type[.=$key/@b])