如何将 return 类别与其子类别在同一级别?
How to return category with its sub-categories which are on the same level?
如何使用 XPath(最好是 1.0)select 类别列表及其子类别的值?
结构如下:
- 类别 (1)
- 姓名 (1)
- 值 (1-1)
- 值 (1-2)
- 类别 (2)
- 姓名 (2)
- 值 (2-1)
- 值 (2-2)
- 值 (2-3)
- 等
所以理想情况下,我需要一个 return 类别名称列表及其相关值,例如:
name
(1): value
(1-1), value
(1-2)
name
(2): value
(2-1), value
(2-2), value
(2-3)
- 等
或者理想情况下:
name
(1) / value
(1-1)
name
(1) / value
(1-2)
name
(2) / value
(2-1)
name
(2) / value
(2-2)
name
(2) / value
(2-3)
我希望是/characteristics/category/[concat(name/uk, "/", value/uk)]
,但是语法不正确,
或任何类似的格式,只要我能区分主要类别和子类别,这样我就可以使用适当的后端语言进一步解析该数据。
基本上我希望实现类别及其子类别之间的关联,以便将它们存储在分层分类词汇表中。
我从提要中得到了以下 XML:
<characteristics>
<category id="1">
<name>
<uk>Orientation</uk>
<es>Orientación</es>
</name>
<value id="1">
<uk>North</uk>
<es>Norte</es>
</value>
<value id="2">
<uk>East</uk>
<es>Este</es>
</value>
<value id="3">
<uk>South</uk>
<es>Sur</es>
</value>
<value id="4">
<uk>West</uk>
<es>Oeste</es>
</value>
</category>
<category id="2">
<name>
<uk>Condition</uk>
<es>Estado</es>
</name>
<value id="1">
<uk>Recently Refurbished</uk>
<es>Renovado Recientemente</es>
</value>
</category>
<category id="3">
<name>
<uk>Pool</uk>
<es>Piscina</es>
</name>
<value id="1">
<uk>Private</uk>
<es>Privada</es>
</value>
</category>
<category id="4">
<name>
<uk>Climate Control</uk>
<es>Climatización</es>
</name>
<value id="1">
<uk>Fireplace</uk>
<es>Chimenea</es>
</value>
</category>
<category id="5">
<name>
<uk>Views</uk>
<es>Vistas</es>
</name>
<value id="1">
<uk>Sea</uk>
<es>Mar</es>
</value>
<value id="2">
<uk>Mountain</uk>
<es>Montaña</es>
</value>
<value id="3">
<uk>Panoramic</uk>
<es>Panorámicas</es>
</value>
</category>
<category id="6">
<name>
<uk>Features</uk>
<es>Caracteristicas</es>
</name>
<value id="1">
<uk>Guest Apartment</uk>
<es>Aprtmnt. Huéspedes</es>
</value>
<value id="2">
<uk>Guest House</uk>
<es>Casa de Huéspedes</es>
</value>
<value id="3">
<uk>Barbeque</uk>
<es>Barbacoa</es>
</value>
</category>
</characteristics>
这是 XPath 查询,其中 returns 值来自第一类:
/characteristics/category[@id=1]/name/uk | /characteristics/category[@id=1]/value/uk
<uk>Orientation</uk>
<uk>North</uk>
<uk>East</uk>
<uk>South</uk>
<uk>West</uk>
现在我该如何组合、比较或反向引用这两个 id
并使其动态适用于每个类别?
知道如何生成这样的 XPath 查询吗?
我也尝试过 ../..
、parent::
、ancestor::
、[]
等表达式和条件的不同组合,但没有成功。我也尝试过使用 string-join
, but it doesn't work in this demo, secondly the code where I'm trying to parse it doesn't support XPath 2.0.
或者 closest/similar selection/format 允许我列出和区分不同类别的名称及其相关值,以便我可以用不同的语言进一步解析该结构?
其他尝试:
concat(/characteristics/category[@id=@id]/name/uk, "/", /characteristics/category[@id=@id]/value/uk)
return仅第一个元素:Orientation/North
(不需要[@id=@id]
)
concat(/characteristics/category/*/*[name()="uk"]/node(), /characteristics/category)
创建了一些我不明白的层次结构(这是什么格式):
Orientation
Orientation
Orientación
North
Norte
East
Este
South
Sur
West
Oeste
但仅限于第一个主要类别
XPath 用于 selection,而不是操作。您可以 select 节点出现在输入 XML 文档中;您不能任意重新排列这些节点,尤其是在 XPath 1.0 中。如果您想重新排列节点,请使用 XSLT 或您调用 XPath 库的其他语言。
更新: 即使在您发表评论并更新您的问题之后,您仍然坚持认为 XPath 1.0 评估的结果可能不仅仅是节点列表 select来自输入XML文档。想要层次结构而不是列表表明您对 XPath 1.0 的期望过高。
最后更新...
XPath 2.0 解决方案
鉴于您的输入 XML,此 XPath 2.0 表达式:
for $c in //category
return for $v in $c/value
return concat($c/name/uk, ' / ', $v/uk)
将产生
Orientation / North
Orientation / East
Orientation / South
Orientation / West
Condition / Recently Refurbished
Pool / Private
Climate Control / Fireplace
Views / Sea
Views / Mountain
Views / Panoramic
Features / Guest Apartment
Features / Guest House
Features / Barbeque
根据要求。
最接近的 XPath 1.0 查询是:
characteristics/category/*/*[name()="uk"]/text()
这将 return 项目的平面列表:
Orientation
North
East
South
West
Condition
Recently Refurbished
...
并且它将 return 以正确的顺序排列所有项目(包括主要类别和子类别),但没有任何层次结构。
缺点是需要在其他地方检测和管理层次结构。例如,在 Drupal CMS 中,我确实将这样的 XML feed 导入到 Taxonomy terms,因此在我手动排列项目的层次结构后,使用现有名称的下一个导入将保持这样的等级制度。
如何使用 XPath(最好是 1.0)select 类别列表及其子类别的值?
结构如下:
- 类别 (1)
- 姓名 (1)
- 值 (1-1)
- 值 (1-2)
- 类别 (2)
- 姓名 (2)
- 值 (2-1)
- 值 (2-2)
- 值 (2-3)
- 等
所以理想情况下,我需要一个 return 类别名称列表及其相关值,例如:
name
(1):value
(1-1),value
(1-2)name
(2):value
(2-1),value
(2-2),value
(2-3)- 等
或者理想情况下:
name
(1) /value
(1-1)name
(1) /value
(1-2)name
(2) /value
(2-1)name
(2) /value
(2-2)name
(2) /value
(2-3)我希望是
/characteristics/category/[concat(name/uk, "/", value/uk)]
,但是语法不正确,
或任何类似的格式,只要我能区分主要类别和子类别,这样我就可以使用适当的后端语言进一步解析该数据。
基本上我希望实现类别及其子类别之间的关联,以便将它们存储在分层分类词汇表中。
我从提要中得到了以下 XML:
<characteristics>
<category id="1">
<name>
<uk>Orientation</uk>
<es>Orientación</es>
</name>
<value id="1">
<uk>North</uk>
<es>Norte</es>
</value>
<value id="2">
<uk>East</uk>
<es>Este</es>
</value>
<value id="3">
<uk>South</uk>
<es>Sur</es>
</value>
<value id="4">
<uk>West</uk>
<es>Oeste</es>
</value>
</category>
<category id="2">
<name>
<uk>Condition</uk>
<es>Estado</es>
</name>
<value id="1">
<uk>Recently Refurbished</uk>
<es>Renovado Recientemente</es>
</value>
</category>
<category id="3">
<name>
<uk>Pool</uk>
<es>Piscina</es>
</name>
<value id="1">
<uk>Private</uk>
<es>Privada</es>
</value>
</category>
<category id="4">
<name>
<uk>Climate Control</uk>
<es>Climatización</es>
</name>
<value id="1">
<uk>Fireplace</uk>
<es>Chimenea</es>
</value>
</category>
<category id="5">
<name>
<uk>Views</uk>
<es>Vistas</es>
</name>
<value id="1">
<uk>Sea</uk>
<es>Mar</es>
</value>
<value id="2">
<uk>Mountain</uk>
<es>Montaña</es>
</value>
<value id="3">
<uk>Panoramic</uk>
<es>Panorámicas</es>
</value>
</category>
<category id="6">
<name>
<uk>Features</uk>
<es>Caracteristicas</es>
</name>
<value id="1">
<uk>Guest Apartment</uk>
<es>Aprtmnt. Huéspedes</es>
</value>
<value id="2">
<uk>Guest House</uk>
<es>Casa de Huéspedes</es>
</value>
<value id="3">
<uk>Barbeque</uk>
<es>Barbacoa</es>
</value>
</category>
</characteristics>
这是 XPath 查询,其中 returns 值来自第一类:
/characteristics/category[@id=1]/name/uk | /characteristics/category[@id=1]/value/uk
<uk>Orientation</uk>
<uk>North</uk>
<uk>East</uk>
<uk>South</uk>
<uk>West</uk>
现在我该如何组合、比较或反向引用这两个 id
并使其动态适用于每个类别?
知道如何生成这样的 XPath 查询吗?
我也尝试过 ../..
、parent::
、ancestor::
、[]
等表达式和条件的不同组合,但没有成功。我也尝试过使用 string-join
, but it doesn't work in this demo, secondly the code where I'm trying to parse it doesn't support XPath 2.0.
或者 closest/similar selection/format 允许我列出和区分不同类别的名称及其相关值,以便我可以用不同的语言进一步解析该结构?
其他尝试:
concat(/characteristics/category[@id=@id]/name/uk, "/", /characteristics/category[@id=@id]/value/uk)
return仅第一个元素:
Orientation/North
(不需要[@id=@id]
)concat(/characteristics/category/*/*[name()="uk"]/node(), /characteristics/category)
创建了一些我不明白的层次结构(这是什么格式):
Orientation Orientation Orientación North Norte East Este South Sur West Oeste
但仅限于第一个主要类别
XPath 用于 selection,而不是操作。您可以 select 节点出现在输入 XML 文档中;您不能任意重新排列这些节点,尤其是在 XPath 1.0 中。如果您想重新排列节点,请使用 XSLT 或您调用 XPath 库的其他语言。
更新: 即使在您发表评论并更新您的问题之后,您仍然坚持认为 XPath 1.0 评估的结果可能不仅仅是节点列表 select来自输入XML文档。想要层次结构而不是列表表明您对 XPath 1.0 的期望过高。
最后更新...
XPath 2.0 解决方案
鉴于您的输入 XML,此 XPath 2.0 表达式:
for $c in //category
return for $v in $c/value
return concat($c/name/uk, ' / ', $v/uk)
将产生
Orientation / North
Orientation / East
Orientation / South
Orientation / West
Condition / Recently Refurbished
Pool / Private
Climate Control / Fireplace
Views / Sea
Views / Mountain
Views / Panoramic
Features / Guest Apartment
Features / Guest House
Features / Barbeque
根据要求。
最接近的 XPath 1.0 查询是:
characteristics/category/*/*[name()="uk"]/text()
这将 return 项目的平面列表:
Orientation
North
East
South
West
Condition
Recently Refurbished
...
并且它将 return 以正确的顺序排列所有项目(包括主要类别和子类别),但没有任何层次结构。
缺点是需要在其他地方检测和管理层次结构。例如,在 Drupal CMS 中,我确实将这样的 XML feed 导入到 Taxonomy terms,因此在我手动排列项目的层次结构后,使用现有名称的下一个导入将保持这样的等级制度。