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

前端 未结 4 1247
悲&欢浪女
悲&欢浪女 2020-12-04 18:59

For example:

//person[@id=\'abc123\']/@haircolor|/@weight\"

PS. there are lots of \"person\" records

相关标签:
4条回答
  • 2020-12-04 19:38

    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>
    
    0 讨论(0)
  • 2020-12-04 19:50

    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)`
    
    0 讨论(0)
  • 2020-12-04 19:51

    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 讨论(0)
  • 2020-12-04 19:56

    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" />
    
    0 讨论(0)
提交回复
热议问题