的xsl:
<ol id="presents">
<xsl:for-each select="tns:meeting/tns:jobs/tns:ausents/tns:ausent-member">
<li id="ausent"><a HREF="#jobs"><b><xsl:value-of select="tns:meeting/tns:members/tns:member[@member_id = current()/@member_id]/@name"/></b></a></li>
</xsl:for-each>
</ol>
xml示例:
<tns:meeting data="2001-01-01" local="Porto" organization="FCUP" type="ordinária" xmlns:tns="http://www.example.org/meeting" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/meeting meeting.xsd ">
<tns:members>
<tns:member name="João" member_id="i1" title="account manager"></tns:member>
<tns:member name="Carlos" member_id="i2" title="director geral"></tns:member>
<tns:member name="José" member_id="i3" title="colaborador"></tns:member>
<tns:member name="Joana" member_id="i4" title="secretária"></tns:member>
<tns:member name="Carolina" member_id="i5" title="tesoureira"></tns:member>
<tns:member name="Pedro" member_id="i6" title="colaborador"></tns:member>
<tns:member name="Ivo" member_id="i7" title="presidente AE"></tns:member>
<tns:member name="Luis" member_id="i8" title="vice-presidente AE"></tns:member>
</tns:members>
<tns:jobs> <!-- Possibilidade de colocar o atributo nome no trabalho -->
<tns:ausents>
<tns:ausent-member member_id="i1">
<tns:justification>Problemas pessoais</tns:justification>
</tns:ausent-member>
<tns:ausent-member member_id="i2">
<tns:justification>Problemas pessoais</tns:justification>
</tns:ausent-member>
</tns:ausents>
步骤
“tns:meeting / tns:members / tns:member [@member_id = 电流()/ @ member_id] “&gt;” 中
无法正常工作,我无法找到问题......
答案 0 :(得分:3)
正是因为这个表达......
<xsl:value-of select="tns:meeting/tns:members/tns:member[@member_id = current()/@member_id]/@name">
此时,您位于xsl:for-each,位于 tns:ausent-member 元素上,但您的 xsl:value-of 是相对表达。它与您所提到的元素相关,因此正在寻找名为 tns:meeting 的子元素。
你需要把它变成绝对表达式
<xsl:value-of select="/tns:meeting/tns:members/tns:member[@member_id = current()/@member_id]/@name">
开头的正斜杠意味着它将从文档节点开始,这就是你想要的。
但是,以这种方式查找元素时,有时最好定义一个键
<xsl:key name="member" match="tns:member" use="@member_id" />
然后你可以像这样查找元素:
<xsl:value-of select="key('member', @member_id)/@name"/>
试试这个XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="http://www.example.org/meeting">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:key name="member" match="tns:member" use="@member_id" />
<xsl:template match="/">
<ol id="presents">
<xsl:for-each select="tns:meeting/tns:jobs/tns:ausents/tns:ausent-member">
<li id="ausent"><a HREF="#jobs"><b>
<xsl:value-of select="key('member', @member_id)/@name"/>
</b></a></li>
</xsl:for-each>
</ol>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:2)
tns:members
和tns:jobs
是兄弟姐妹。在xsl:for-each select="tns:meeting/tns:jobs/tns:ausents/tns:ausent-member"
内部节点内部是tns:ausent-member
,因此您需要前往
<xsl:value-of select="ancestor::tns:meeting/tns:members/tns:member[@member_id = current()/@member_id]/@name">