[英]In XSLT 1.0, how can I access attributes of a particular XML element when it is one of multiple elements with the same name?
I blame the Google Search Appliance for making me ask this question. 我责怪Google Search Appliance让我问这个问题。
Here is a snippet of the XML returned by the Appliance: 这是设备返回的XML的片段:
<GSP VER="3.2">
<TM>0.073846</TM>
<Q>test</Q>
<PARAM name="entqr" value="0" original_value="0"/>
<PARAM name="access" value="p" original_value="p"/>
<PARAM name="output" value="xml_no_dtd" original_value="xml_no_dtd"/>
<PARAM name="sort" value="date:D:L:d1" original_value="date%3AD%3AL%3Ad1"/>
<PARAM name="ud" value="1" original_value="1"/>
<PARAM name="ie" value="UTF-8" original_value="UTF-8"/>
<PARAM name="btnG" value="Search" original_value="Search"/>
<PARAM name="client" value="default_frontend" original_value="default_frontend"/>
<PARAM name="oe" value="UTF-8" original_value="UTF-8"/>
<PARAM name="q" value="I like stuff" original_value="I like stuff"/>
...
I need to do an xsl:value-of for a specific one of those PARAM elements, conditionally based on its name. 我需要根据其名称有条件地为这些PARAM元素中的特定元素执行xsl:value-of。 eg I need to output the @value for the PARAM element with @name="client". 例如,我需要使用@ name =“ client”为PARAM元素输出@value。
Thanks! 谢谢!
You can declare xsl:key as a top-level element: 您可以将xsl:key声明为顶级元素:
<xsl:key name="param" match="PARAM" use="@name"/>
and then use key(key-name,value) function. 然后使用key(key-name,value)函数。
<xsl:value-of select="key('param','q')/@value"/>
It takes time to 'init' key but further it's much faster than selecting node[predicate] each time. “初始化”密钥需要时间,但比每次选择node [predicate]都要快得多。 So it's better to use it when you need to access PARAMs multiple times. 因此,当您需要多次访问PARAM时,最好使用它。
http://www.w3.org/TR/xslt#key http://www.w3.org/TR/xslt#key
Also, knowing your tree, you can match your nodes (PARAM) more accurately. 此外,了解您的树后,您可以更准确地匹配节点(PARAM)。
Try using predicates on your XPath statements! 尝试在XPath语句上使用谓词 ! try something like: 尝试类似:
<xsl:value-of select="PARAM[@name='output']/@value"/>
<xsl:value-of select="//PARAM[@name='client']/@value" />
You didn't add the complete XML document. 您没有添加完整的XML文档。 In case there is a default namespace involved you will have to declare a prefix that you want to use and prepend that to the element and attribute names respectively. 如果涉及默认的名称空间,则必须声明要使用的前缀,并将其分别添加到元素名称和属性名称之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.