开发者

How to select two attributes from the same node with one expression in XPath?

For example:

//person开发者_Go百科[@id='abc123']/@haircolor|/@weight"

PS. there are lots of "person" records


Try this:

//person[@id='abc123']/@*[name()='weight' or name()='haircolor']

If you're using an XPath 2.0 processor, you may also use a prettier option:

//person[@id='abc123']/(@haircolor|@weight)`


Are you wanting to search for person nodes based on the value of multiple attributes. If that's the question then you can just use ands e.g.

//person[@id='abc123' and @haircolor='blue' and @weight='...']

If you want to search on a single attribute, but return the values of the other attributes, I would do something like this:

 <xsl:template match="person[@id='abc123']">
     <xsl:value-of select="@haircolor"/>
     <xsl:value-of select="@weight"/>
  </xsl:template>


If you are trying to get the values of the specified attributes I would suggest introducing a variable for the requested person.

<xsl:variable name="person" select="//person[@id = 'abc123']" />

After that you can get any attribute from the requested person by using the specified variable.

<xsl:value-of select="$person/@haircolor" />
<xsl:value-of select="$person/@weight" />


Sample XML:

<X>
<Y ATTRIB1=attrib1_value ATTRIB2=attrib2_value/>
</X>

string xPath="/" + X + "/" + Y +
"[@" + ATTRIB1 + "='" + attrib1_value + "']" +
"[@" + ATTRIB2 + "='" + attrib2_value + "']"

XPath Testbed: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜