使用 XPath 获取属性 [英] Getting attribute using XPath

查看:46
本文介绍了使用 XPath 获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个像这样的 XML 结构:

Given an XML structure like so:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>

如何获取 lang 的值(其中 lang 是书名中的 eng),对于第一个元素?

How could I get the value of lang (where lang is eng in book title), for the first element?

推荐答案

我如何获得 lang 的值(书名中的 lang=eng),对于第一个元素?

How could I get the value of lang (where lang=eng in book title), for the first element?

使用:

/*/book[1]/title/@lang

这意味着:

选择作为 XML 文档顶部元素的第一个 book 子元素的子元素的 title 元素的 lang 属性.

Select the lang attribute of the title element that is a child of the first book child of the top element of the XML document.

要仅获取此属性的字符串值,请使用标准 XPath 函数 string():

string(/*/book[1]/title/@lang)

这篇关于使用 XPath 获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆