代码之家  ›  专栏  ›  技术社区  ›  nont

为什么不能在这个xpath表达式中使用子元素进行导航?

  •  1
  • nont  · 技术社区  · 15 年前

    在XSL 2.0中,我试图用不同的值迭代一些数据,然后对它们做一些事情。

    <xsl:for-each select="distinct-values(InvoiceLine/Service/ServiceMnemonicCode)">                
      <xsl:variable name="mnemonic">
        <xsl:value-of select="."/>
      </xsl:variable>
            <fo:table-row>
                <fo:table-cell>
                    <fo:block>
    <xsl:value-of select="InvoiceLine/Service[ServiceMnemonic=$mnemonic]/ServiceDescription"/>                      
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
    </xsl:for-each>
    

    但是,最后出现以下错误:

    XPTY0020: Axis step
     child::element({http://schemas.blabla.com/etp/invoice/types}InvoiceLine, xs:anyType)
     cannot be used here: the context item is an atomic value
    ailed to compile stylesheet. 1 error detected.
    

    我疯狂地谷歌,我确实看到人们抱怨“原子值”,但我没有看到任何人建议如何处理它。我用的是萨克森9。任何见解都将受到极大的赞赏。

    1 回复  |  直到 11 年前
        1
  •  0
  •   nont    15 年前

    不知道它是否是最佳解决方案,但这似乎有效:

    <xsl:template match="Invoice">
            <xsl:variable name="invoice">
                <xsl:copy-of select="."/>
            </xsl:variable>
            <fo:table border="0.5pt solid black" text-align="center">
                <fo:table-body>
                    <xsl:for-each select="distinct-values(InvoiceLine/Service/ServiceMnemonicCode)">
                        <xsl:sort/>
                        <xsl:variable name="code">
                            <xsl:copy-of select="."/>
                        </xsl:variable>
                        <fo:table-row>
                            <fo:table-cell>
                                <fo:block>
                                    <xsl:value-of select="($invoice/node()/InvoiceLine/Service[ServiceMnemonicCode=$code]/ServiceDescription)[1]"/>
                                </fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                    </xsl:for-each>
                </fo:table-body>
            </fo:table>
        </xsl:template>