XSLT - How to select XML Attribute by Attribute?

前端 未结 5 638
陌清茗
陌清茗 2020-12-25 12:20

this is the structure of my source xml:






        
相关标签:
5条回答
  • 2020-12-25 12:47

    Note: using // at the beginning of the xpath is a bit CPU intensitve -- it will search every node for a match. Using a more specific path, such as /root/DataSet will create a faster query.

    0 讨论(0)
  • 2020-12-25 12:52

    Just remove the slash after Data and prepend the root:

    <xsl:variable name="myVarA" select="/root/DataSet/Data[@Value1='2']/@Value2"/>
    
    0 讨论(0)
  • 2020-12-25 12:53

    Try this

    xsl:variable name="myVarA" select="//DataSet/Data[@Value1='2']/@Value2" />
    

    The '//' will search for DataSet at any depth

    0 讨论(0)
  • 2020-12-25 12:54

    There are two problems with your xpath - first you need to remove the child selector from after Data like phihag mentioned. Also you forgot to include root in your xpath. Here is what you want to do:

    select="/root/DataSet/Data[@Value1='2']/@Value2"
    
    0 讨论(0)
  • 2020-12-25 13:08

    I would do it by creating a variable that points to the nodes that have the proper value in Value1 then referring to t

    <xsl:variable name="myVarANode" select="root//DataSet/Data[@Value1='2']" />
    <xsl:value-of select="$myVarANode/@Value2"/>
    

    Everyone else's answers are right too - more right in fact since I didn't notice the extra slash in your XPATH that would mess things up. Still, this will also work , and might work for different things, so keep this method in your toolbox.

    0 讨论(0)
提交回复
热议问题