我正在过滤一个大文件,其中包含适合儿童,男人和女人的各种鞋子.
现在我想过滤掉某些类型的女鞋,下面的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')]
Run Code Online (Sandbox Code Playgroud)
编辑:添加了请求的文件样本
<Products>
<!-- snip -->
<Product ProgramID="4875" ArticleNumber="GO1-f05-0001-12">
<CategoryPath>
<ProductCategoryID>34857489</ProductCategoryID>
<ProductCategoryPath>Damen > Sale > Schuhe > Sneaker > Sneaker Low</ProductCategoryPath>
<AffilinetProductCategoryPath>Kleidung & Accessoires?</AffilinetProductCategoryPath>
</CategoryPath>
<Price>
<DisplayPrice>40.95 EUR</DisplayPrice>
<Price>40.95</Price>
</Price>
</Product>
<!-- snip -->
</Products>
Run Code Online (Sandbox Code Playgroud)
如果你有XPath 2.0可用,你应该尝试这个matches()功能,或者甚至tokenize()是Ranon在他的答案中所建议的.
使用XPath 1.0,缩短表达式的一种方法可能是:
/Products/Product[
CategoryPath/ProductCategoryPath[
contains(., 'Damen')
and ( contains(., 'Halbschuhe')
or contains(., 'Sneaker')
or contains(., 'Ballerinas') )] ]
Run Code Online (Sandbox Code Playgroud)
一个方便的oneliner,更容易复制粘贴:
/Products/Product[CategoryPath/ProductCategoryPath[contains(.,'Damen') and (contains(.,'Halbschuhe') or contains(.,'Sneaker') or contains(.,'Ballerinas'))]]
Run Code Online (Sandbox Code Playgroud)
我试图保持你的表达式的确切方式,没有任何改变应该以任何方式改变行为.
有一些甚至更短的解决方案必须对XML结构等进行假设,但是如果没有完整的上下文,我们无法看到那些隐藏的方式,所以我们不会这样做.
| 归档时间: |
|
| 查看次数: |
15732 次 |
| 最近记录: |