XPath的1查询和属性名 [英] XPath 1 query and attributes name

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

问题描述

第一个问题:有没有办法让在名称的节点的属性

 <节点ATTRIBUTE1 =值1attribute2 =值2/>

第二个问题:是否有一种方式来获得属性和值的值对?情况是这样的:

 <节点ATTRIBUTE1 =10attribute2 =0/>

我想获得所有属性值在哪里> 0,这样说:ATTRIBUTE1 = 10。


解决方案

  

第一个问题:有没有什么办法
  获取一个节点的属性的名称?


  
  

<节点ATTRIBUTE1 =值1
  attribute2 =值2/>


是:
这个XPath前pression
(当节点是上下文(电流)节点)):

     的域名(@ * [1])

产生的第一个属性的名称(排序可能实现 - 依赖)

这个XPath的前pression (当节点是上下文(电流)节点)):

     的域名(@ * [2])

产生第二个属性的名称(排序可能实现 - 依赖)。


  

第二个问题:是否有一种方式来获得
  属性和值的值对?
  情况是这样的:


  
  

<节点ATTRIBUTE1 =10attribute2 =0
  />


  
  

我想获得的所有属性,其中
  值> 0,这样说:ATTRIBUTE1 = 10


此XPath前pression (时名为 ATTRIBUTE1 属性是上下文(电流)节点)):

     CONCAT(名称(),'=',)

生成字符串:

     ATTRIBUTE1 =值

这个XPath的前pression (当节点节点是上下文(电流)节点)):

     @ * [。 > 0]

选择上下文节点,其值是一个数的所有属性,大于0

在XPath 2.0可以在一个单一的XPath前pression将它们结合起来

     @ * [数字&GT(); 0] / CONCAT(名称(。),'=',。)

获得(在这种特定情况下),这样的结果:

     ATTRIBUTE1 = 10

如果您正在使用XPath 1.0 ,它是那么强大,你的需要嵌入在宿主语言中的XPath前pression,如XSLT 。下面的XSLT 1.0 thransformation:

 <的xsl:样式版本=1.0
    的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform>
    < XSL:输出方法=TEXT/>  <的xsl:模板匹配=/ *>
      <的xsl:for-每个选择=@ * [数大于0()>
        <的xsl:value-of的选择=CONCAT(名称(),'=',...)/>
      < /的xsl:for-每个>
    < / XSL:模板>
< / XSL:样式>

在这个XML文档施加

 <节点ATTRIBUTE1 =10attribute2 =0/>

生成完全相同的结果

     ATTRIBUTE1 = 10

First question: is there any way to get the name of a node's attributes?

<node attribute1="value1" attribute2="value2" />

Second question: is there a way to get attributes and values as value pairs? The situation is the following:

<node attribute1="10" attribute2="0" />

I want to get all attributes where value>0 and this way: "attribute1=10".

解决方案

First question: is there any way to get the name of a node's attributes?

<node attribute1="value1" attribute2="value2" />

Yes: This XPath expression (when node is the context (current) node)):

    name(@*[1])

produces the name of the first attribute (the ordering may be implementation - dependent)

and this XPath expression (when node is the context (current) node)):

    name(@*[2])

produces the name of the second attribute (the ordering may be implementation - dependent).

Second question: is there a way to get attributes and values as value pairs? The situation is the following:

<node attribute1="10" attribute2="0" />

I want to get all attributes where value>0 and this way: "attribute1=10".

This XPath expression (when the attribute named "attribute1" is the context (current) node)):

    concat(name(), '=', .)

produces the string:

    attribute1=value1

and this XPath expression (when the node node is the context (current) node)):

    @*[. > 0]

selects all attributes of the context node, whose value is a number, greater than 0.

In XPath 2.0 one can combine them in a single XPath expression:

    @*[number(.) > 0]/concat(name(.),'=',.)

to get (in this particular case) this result:

    attribute1=10

If you are using XPath 1.0, which is less powerful, you'll need to embed the XPath expression in a hosting language, such as XSLT. The following XSLT 1.0 thransformation :

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

  <xsl:template match="/*">
      <xsl:for-each select="@*[number(.) > 0]">
        <xsl:value-of select="concat(name(.),'=',.)"/>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<node attribute1="10" attribute2="0" />

Produces exactly the same result:

    attribute1=10

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

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