首页 > 解决方案 > 是否可以选择 XPATH 节点的属性?

问题描述

我有一个 XML 格式:

<articleslist>
    <articles>
        <originalId>507948</originalId>
        <title>Hogan Lovells Training Contract</title>
        <slug>hogan-lovells-training-contract</slug>
        <metaTitle>Hogan Lovells Training Contract</metaTitle>
        <metaDescription>Find out about the Hogan Lovells Training Contract and Application Process</metaDescription>
        <language>en</language>
        <disableAds>false</disableAds>
        <shortUrl>false</shortUrl>
        <category_slug>law</category_slug>
        <subcategory_slug>industry</subcategory_slug>
        <updatedAt>2021-03-15T18:38:51.058+00:00</updatedAt>
        <createdAt>2018-11-29T06:42:51.665+00:00</createdAt>
    </articles>
</articlelist>

我可以使用 XPATH 选择行值//articles

如何选择文章的子属性(即列标题),所以我得到一个表单列表:

originalId
title
slug
etc...

标签: xpath

解决方案


取决于您的 XPath 版本。

在 XPath 2.0 中,它很简单//articles/*/name()

在 1.0 中这是不可能的,因为没有像“字符串序列”这样的数据类型。您必须将元素集返回为//articles/*,然后在调用程序中提取它们的名称。


推荐阅读