0

你能告诉我为什么它不起作用吗?我是 xsl 的新手....

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 = current()/@member_id]">"

不工作,我找不到问题....

4

2 回答 2

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>
于 2013-10-27T15:28:32.280 回答
2

嗯,tns:memberstns: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"> 
于 2013-10-27T15:25:16.333 回答