简体   繁体   English

在XSLT 1.0中,当特定XML元素是多个具有相同名称的元素之一时,如何访问它的属性?

[英]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.

相关问题 当我们有多个具有相同名称但属性不同的元素时,如何使用Xdocument从xml中删除元素 - How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes 如何使用 XSLT 合并 XML 元素,然后构造相同名称/类型的元素/属性? - How do I use XSLT to merge XML elements and then structure elements/attributes of the same name/type? 如何使用XSLT1.0在XML文件中替换ID元素及其所有参考元素 - How can I replace an ID element AND all of its reference elements in an XML file using XSLT1.0 xslt 1.0-使用xslt 1.0如何使xslt适用于多个文档元素 - xslt 1.0- Using xslt 1.0 how can I make the xslt work for multiple document elements 如何使用 xslt 1.0 在 xml 元素中划分字符串 - How can I divide string in xml element using xslt 1.0 基本XML / XSLT - 当存在多个同名元素时的值 - Basic XML/XSLT - value-of when there are multiple elements of the same name 如何使用XSLT 1.0复制XML中的特定元素 - How to copy particular element in an XML using XSLT 1.0 XML中多个具有相同名称的元素,并且仅希望使用xslt根据一种条件更改一个特定元素的值 - Multiple elements with same name in XML and want to change value of one specific element only based on one condition using xslt 如何访问XML元素和属性? - How can I access XML elements and attributes? 使用XSLT 1.0,如何编写一个模板来处理来自平面XML记录的编号属性? - Using XSLT 1.0, how do I write one template to handle numbered attributes from a flat XML record?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM