使用XPath访问XML节点

RVK*_*RVK 0 xml xpath

<attributes>
  <attribute name="Mail Zone" property="alerts.p45" type="">
    <value>mailzone</value>
  </attribute>
  <attribute name="Employee Name" property="acm_alert_custom_attributes.cs11" type="">
    <value>employeename</value>
  </attribute>
  <attribute name="Manager Name" property="alerts.p23" type="">
    <value><managername></value>
  </attribute>
Run Code Online (Sandbox Code Playgroud)

如何<value>使用XPath基于上述XML的属性"name" 选择节点?

Wel*_*bog 6

假设您想要选择名称为"Employee Name" value的子元素attribute.

XPath表达式如下:

/attributes/attribute[@name="Employee Name"]/value
Run Code Online (Sandbox Code Playgroud)

在XSL中,您可以像这样使用它:

<xsl:value-of select="/attributes/attribute[@name='Employee Name']/value"/>
Run Code Online (Sandbox Code Playgroud)

它以下列方式构建.首先,您要选择一个value属性,其父attribute元素是一个元素,其父元素是根(我假设)attributes.该/运营商表示元素之间的父子关系.因此,选择所有 value元素的表达式如下:

/attributes/attribute/value
Run Code Online (Sandbox Code Playgroud)

以此为基础,您希望通过其他属性过滤结果.在这种情况下,您希望按元素的name属性进行过滤attribute(您选择的名称可能会使事情难以理解).使用[]要过滤的元素上的子句进行过滤.在你的情况下,这是attribute元素:

/attributes/attribute[]/value 
Run Code Online (Sandbox Code Playgroud)

现在你需要在过滤器中添加一些东西.该@符号表示你被过滤元素,其次是你想要的属性的名称的属性.然后将属性与某个已知值进行比较,得到上面的表达式:

/attributes/attribute[@name='filter']/value
Run Code Online (Sandbox Code Playgroud)