我正在尝试使用 XSLT 转换来转换 XML 文档,并且想知道是否可以获取具有另一个属性值的 XML 元素。
<Question>
<text id="154014">Email</text>
<answer><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d5c5f5e2d0a000c0401430e0200" rel="noreferrer noopener nofollow">[email protected]</a></answer>
</Question>
<Question>
<text id="154015">Phone</text>
<answer>123456</answer>
</Question>
所以我的问题是,如果我的 text/@id 值为 154014,我需要得到答案 [email protected]
我无法使用 <xsl:value-of select="text[@id='154014']" />
,因为它将采用不需要的 xml 元素。
最佳答案
实现此目的的另一种方法是通过 key 。像这样定义一个键,通过 text/@id
Question
元素
<xsl:key name="questions" match="Question" use="text/@id" />
然后要获取 id 的相关答案,请执行以下操作...
<xsl:value-of select="key('questions', '15184')/answer" />
或者,使用参数而不是硬编码值
<xsl:value-of select="key('questions', $id)/answer" />
关于java - 使用 XSLT 中的 id 选择 XML 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53616163/