3

我正在过滤一个大文件,其中包含儿童鞋的类型,男人和女人。

现在我想过滤掉某些类型的女鞋,下面的 xpath 可以工作,但是我正在使用的程序有 xpath 长度限制。所以我想知道是否有更短/更有效的方法来构建这个 xpath

/Products/Product[contains(CategoryPath/ProductCategoryPath,'Halbschuhe') and contains(CategoryPath/ProductCategoryPath,'Damen') or  contains(CategoryPath/ProductCategoryPath,'Sneaker') and contains(CategoryPath/ProductCategoryPath,'Damen') or contains(CategoryPath/ProductCategoryPath,'Ballerinas') and contains(CategoryPath/ProductCategoryPath,'Damen')]

编辑:添加了请求的文件示例

<Products>
    <!-- snip -->
    <Product ProgramID="4875" ArticleNumber="GO1-f05-0001-12">
        <CategoryPath>
            <ProductCategoryID>34857489</ProductCategoryID>
            <ProductCategoryPath>Damen &gt; Sale &gt; Schuhe &gt; Sneaker &gt; Sneaker Low</ProductCategoryPath>
            <AffilinetProductCategoryPath>Kleidung &amp; Accessoires?</AffilinetProductCategoryPath>
        </CategoryPath>
        <Price>
            <DisplayPrice>40.95 EUR</DisplayPrice>
            <Price>40.95</Price>
        </Price>
    </Product>
    <!-- snip -->
</Products>
4

2 回答 2

7

如果您有 XPath 2.0 可用,您应该尝试该matches()功能,甚至tokenize()按照 Ranon 在他的出色回答中所建议的那样。

使用 XPath 1.0,缩短表达式的一种方法是:

/Products/Product[
    CategoryPath/ProductCategoryPath[
        contains(., 'Damen')
            and (  contains(., 'Halbschuhe')
                or contains(.,    'Sneaker')
                or contains(., 'Ballerinas') )] ]

方便的 oneliner,便于复制粘贴:

/Products/Product[CategoryPath/ProductCategoryPath[contains(.,'Damen') and (contains(.,'Halbschuhe') or contains(.,'Sneaker') or contains(.,'Ballerinas'))]]

我试图准确地保留您的表达方式,任何更改都不应该以任何方式改变行为。

有一些甚至更短的解决方案必须对 XML 结构等进行假设,但这些解决方案可能会以某种隐藏的方式被破坏,如果没有完整的上下文,我们就无法看到,所以我们不会那样做。

于 2013-02-08T12:49:06.363 回答
2

如果您的 XPath 引擎支持 XPath 2.0,则可以以更方便(并且可能更高效)的方式完成它:

//Product[
  CategoryPath/ProductCategoryPath[
    tokenize(., '\s') = ('Halbschuhe', 'Sneaker', 'Ballerinas') and contains(., 'Damen')
  ]
]

fn:tokenize($string, $token)在正则表达式上拆分字符串(此处使用空格,您也可以仅提供空格)。=在基于集合的语义上进行比较,因此如果左侧的任何字符串等于右侧的任何字符串,则返回 true。

于 2013-02-08T13:03:32.277 回答