首页 > 解决方案 > 如何计数()属性中与另一个不在同一lv中的元素?

问题描述

尽管我已经尝试了很多方法来编写这些 count() 行,但我无法让我的 Xslt 表正常工作。

在查看我的尝试之前,请查看下面的代码。我试过了:(1)

<xsl:for-each select="Trans/Episode/Section/Turn/tour">
<tr>
   <td><xsl:value-of select="count(motBDL[@lexeme='JE'])" /></td>
</tr>
</xsl:for-each>

(2)

<td><xsl:value-of select="count(motBDL[lexeme='JE'])" /></td>

(3)

<xsl:for-each select="Trans">
<td><xsl:value-of select="count(/Episode/Section/Turn/tour/motBDL[@lexeme='JE'])" /></td>

(4)`

与 (5;6;7;8)"()" 而不是 "[]" 相同

XML =

   <!--The entire code is correct (just for you to understand what I am trying to extract)-->
    <?xml version="1.0" encoding="UTF8" standalone="yes"?>
    <?xml-stylesheet type="text/xsl" href="Projet-Info.xsl"?>
    <!DOCTYPE Trans SYSTEM "trans-corpus.dtd">
    <Trans scribe="computer-name" audio_filename="Debat Hollande Sarkozy 1998" video_filename="" version="8" version_date="181221">
    <Speakers>
        <Speaker name="Nicolas Sarkozy" check="yes"/>
        <Speaker name="François Hollande" check="yes"/>
        <Speaker name="Journaliste" check="no"/>
        <Speaker name="Journaliste 2" check="no"/>
      </Speakers>
      <Episode>
        <Section type="report" startTime="0" endTime="1408.652">
            <Sync time="0.152"/>
            <Turn speaker="spk1" startTime="0.152" endTime="3.038">
            <tour nbmots="6" id="000000">
              <motBDL lexeme="POUR" phon="puʁ">pour</motBDL>
              <motBDL lexeme="MOI" phon="mwa">moi</motBDL>
              <motBDL lexeme="JE" phon="ʒə">je</motBDL>
              <motBDL lexeme="NE" phon="nə">ne</motBDL>
              <motBDL lexeme="SAVOIR" phon="save">savais</motBDL>
              <motBDL lexeme="PAS" phon="pa">pas</motBDL>
            </tour>
         </Turn>
        </Section>
      </Episode>
    </Trans>

XSL =

<!--Code b4 is correct-->
<table border="2" style="text-align: center;">
  <tr>
   <!--<th>nom</th>-->
   <th>je</th>
  </tr>
 <xsl:for-each select="Trans">
 <xsl:if test="not(@check='no')">
   <tr>
    <td><xsl:value-of select="Speakers/Speaker/@name" /></td>
    <td><xsl:value-of select="count(Episode/Section/Turn/tour/motBDL[@lexeme='JE'])" /></td>
   </tr>
 </xsl:if>
 </xsl:for-each>
</table>
<!--Code after is correct-->
<!--I expect when I run my program on firefox to actually run.-->

标签: xslt

解决方案


你已经用一个正斜杠开始你的表达......

/Episode/Section/Turn/tour/motBDL[@lexeme='JE']

这意味着 xpath 表达式将相对于文档节点,而不是相对于您当前所在的节点。你应该做这个...

<xsl:value-of select="count(Episode/Section/Turn/tour/motBDL[@lexeme='JE'])" />

你也有这个陈述同样的问题

<xsl:value-of select="/Speakers/Speaker/@nom" />

这也是指一个不存在的属性。如果您打算在@name这里写,请记住在 XSLT 1.0 中,如果您这样做xsl:value-of并选择多个节点,那么只会输出第一个节点。

另外,请注意,您的 XSLT 中有两个?xml-stylesheet指令。我不确定这是允许的......


推荐阅读