首页 > 解决方案 > 如何使用 XPath 提取部分文本并将它们作为键值对放入 woocommerce 属性中?

问题描述

我想提取单词(文本)作为给定 XML 示例的键:值对:

<description>
[Партиден номер]: 2UW01AA [Номер на модела]: HP 14.1 Business Sleeve [Line]: Business [Screen size]: 14.1&quot; [Material]: Polyester [Color]: Black [Dimensions]: [more]
</description>

我想要这样的结果:

Партиден номер 2UW01AA
Номер на модела HP 14.1 Business Sleeve
Line Business
Screen size 14.1&quot;
Material Polyester
Color Black

我想知道第一个和第二个单词的 Xpath 作为键:值对插入到 woocommerce 属性中。需要两个 Xpath 查询以获得结果。

translate(substring-before(substring-after(//description,"["),": ["),"[]:",codepoints-to-string(10) ) 

这给了我正确的输出,但我需要两个 Xpath 查询,一个用于第一个单词,一个用于第二个单词。

标签: xmlxpath

解决方案


对于值(translate将返回一个包含多行的字符串,而tokenize将返回一个字符串列表):

translate(replace(replace(replace(replace(substring-before(normalize-space(//description)," [more"), "\[(.*?)\]:", ";")," ; ",";")," ;",""),"; ",""),";",codepoints-to-string(10))

tokenize(replace(replace(replace(replace(substring-before(normalize-space(//description)," [more"), "\[(.*?)\]:", ";")," ; ",";")," ;",""),"; ",""),";")

输出 :

2UW01AA
HP 14.1 Business Sleeve
Business
14.1"
Polyester
Black

对于键:

translate(substring-before(substring-after(replace(normalize-space(//description),"\]: (.+?) \[",";"),"["),"]"),";",codepoints-to-string(10))

tokenize(substring-before(substring-after(replace(normalize-space(//description),"\]: (.+?) \[",";"),"["),"]"),";")

输出 :

Партиден номер
Номер на модела
Line
Screen size
Material
Color
Dimensions

注意:当然,前面的表达式可以进行很多优化。


推荐阅读