简体   繁体   English

xslt - 选择具有特定子节点的节点

[英]xslt - select node with specific subnode

I have an xml:我有一个 xml:

<mynodes>
   <mynode>
     <element1>a</element1>
     <element2>b</element2>
   </mynode>
   <mynode>
     <element1>c</element1>
     <element2>d</element2>
   </mynode>
</mynodes>
<translation>
  <col>element2</col>
  <cont>d</cont>
</translation>

I need an xpath, which gives me the the element1 content of the same subnode.我需要一个 xpath,它给我同一个子节点的 element1 内容。

I have tried different xpaths, always with error.我尝试过不同的 xpath,总是有错误。

My idea was to:我的想法是:

<xsl:variable name="col" select="/translation/col" />
<xsl:variable name="cont" select="/translation/cont" />
<xsl:value-of select="/mynodes/mynode[./name()=$col and ./text()=$cont]/element1" />

I have tried many things.我尝试了很多东西。 Any idea?任何的想法? I did not want to loop the structure, because the structure is quite big and there are several queries to do.我不想循环结构,因为结构相当大并且有几个查询要做。

使用<xsl:value-of select="/mynodes/mynode[*[name()=$col and . = $cont]]/element1"/>

XSLT has a built-in key mechanism for resolving cross-references - and it would be best to use it, esp. XSLT 具有用于解决交叉引用的内置密钥机制 - 最好使用它,尤其是。 if you have "several queries to do".如果您有“几个查询要做”。

Here you could define the key as:在这里,您可以将密钥定义为:

<xsl:key name="k1" match="*" use="concat(name(), '|', .) " />

and then call it from the context of translation as::然后从translation的上下文中调用它为:

<xsl:value-of select="key('k1', concat($col, '|', $cont))/../element1"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM