xml - 如何使用 XPath 提取部分文本并将它们作为键值对放入 woocommerce 属性中?
问题描述
我想提取单词(文本)作为给定 XML 示例的键:值对:
<description>
[Партиден номер]: 2UW01AA [Номер на модела]: HP 14.1 Business Sleeve [Line]: Business [Screen size]: 14.1" [Material]: Polyester [Color]: Black [Dimensions]: [more]
</description>
我想要这样的结果:
Партиден номер 2UW01AA
Номер на модела HP 14.1 Business Sleeve
Line Business
Screen size 14.1"
Material Polyester
Color Black
我想知道第一个和第二个单词的 Xpath 作为键:值对插入到 woocommerce 属性中。需要两个 Xpath 查询以获得结果。
translate(substring-before(substring-after(//description,"["),": ["),"[]:",codepoints-to-string(10) )
这给了我正确的输出,但我需要两个 Xpath 查询,一个用于第一个单词,一个用于第二个单词。
解决方案
对于值(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
注意:当然,前面的表达式可以进行很多优化。
推荐阅读
- html - 如何使用基于 DOM 元素的 AngularJS 隐藏元素
- arrays - 使用 powershell 在数组字符串中创建换行符
- git - 来自詹金斯管道的 Git 推送
- c# - 处理大量数据加载到列表视图中并定期重新填充以进行搜索的最佳方法
- meteor - 未捕获的类型错误:require.ensure 不是 Object.getComponent 的函数(ECMASCRIPT)
- javascript - 如何根据多个变量对数据进行分类
- sql - 选择 HR 模式上的 pl-sql 错误
- java - 如何拥有无限的命令行参数
- amazon-web-services - 如何在 AWS API Gateway 中自定义 cognito 用户池授权方的 Unauthorized 401 JSON 响应
- python - 在python项目中的vs代码中设置根文件夹