与
的XML面对面时<entity1>
<name>2345</name>
<type notNull="1" size="normal">select</type>
<value>D</value>
<options>
<option />
<option>
<value>A</value>
<show>Alpha</show>
</option>
<option>
<value>B</value>
<show>Beta</show>
</option>
<option>
<value>G</value>
<show>Gamma</show>
</option>
<option>
<value>D</value>
<show>Delta</show>
<selected />
</option>
</entity1>
如何从Entity1中提取" delta",基于值或"&lt; selected/>" tag?
谢谢!
假设当前上下文节点是entity1
您可以使用
<xsl:value-of select="options/option[selected]/show" />
根据<selected/>
元素的存在或
<xsl:value-of select="options/option[value=current()/value]/show"/>
基于匹配<value>
(current()
函数为您提供当前上下文节点,即select
表达式顶部的.
,在这种情况下,entity1
)。
您可以使用变量,如下:
<xsl:template match="/entity1">
<xsl:variable name="selected" select="value"></xsl:variable>
<xsl:value-of select="options/option[value=$selected]/show"/>
</xsl:template>
对于散装查找,更好的选择是使用键
<xsl:key name="selected" match="/entity1/options/option" use="value" />
<xsl:template match="/entity1">
<xsl:value-of select="key('selected', value)/show"></xsl:value-of>
</xsl:template>