-1

顧客ごとの請求書の合計を考え出す方法が必要です。(3つの請求書を追加し、それに応じて合計を表示します)、sum(/ * /(PriceUnit * Ordered))を試しましたが、機能しないエラーです。 、.---[請求書の合計=PriceUnit * Ordered] ,. 3つの請求書を合計して結果を表示します。私にとっては大変なので、助けてください

サンプル入力ドキュメント

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Nom.xslt"?>
<customers>
  <customer>
    <clientname>troy madison</clientname>
    <invoices>
    <invoiceDate>8/8/98</invoiceDate>
      <product>
         <PriceUnit>1000</PriceUnit>
         <Ordered>2</Ordered>
     </product>
     <product>
         <PriceUnit>5400</PriceUnit>
         <Ordered>3</Ordered>
     </product>
   </invoices>
   <invoices>
     <invoiceDate>1/4/98</invoiceDate>
       <product>
          <PriceUnit>300</PriceUnit>
          <Ordered>4</Ordered>
       </product>
      <product>
        <PriceUnit>6000</PriceUnit>
        <Ordered>1</Ordered>
     </product>
   </invoices>
  <invoices>
    <invoiceDate>03/5/99</invoiceDate>
      <product>
        <PriceUnit>549</PriceUnit>
        <Ordered>1</Ordered>
     </product>
     <product>
       <PriceUnit>320</PriceUnit>
       <Ordered>2</Ordered>
    </product>
   </invoices>
 </customer>
 <customer>
   <clientname>Morris</clientname>
    <invoices>
      <invoiceDate>1/1/00</invoiceDate>
        <product>
           <PriceUnit>59</PriceUnit>
           <Ordered>3</Ordered>
        </product>
      <product>
          <PriceUnit>55</PriceUnit>
          <Ordered>1</Ordered>
      </product>
    </invoices>
    <invoices>
     <invoiceDate>11/1/01</invoiceDate>
       <product>
         <PriceUnit>10</PriceUnit>
         <Ordered>2</Ordered>
      </product>
      <product>
         <PriceUnit>54</PriceUnit>
         <Ordered>1</Ordered>
       </product>
    </invoices>
    <invoices>
      <invoiceDate>03/2/01</invoiceDate>
        <product>
          <PriceUnit>30</PriceUnit>
          <Ordered>1</Ordered>
        </product>
       <product>
         <PriceUnit>299</PriceUnit>
         <Ordered>1</Ordered>
       </product>
    </invoices>
</customer>
</customers>

期待される出力

[ここに期待される出力を一覧表示するOP。]

これまでに試したスタイルシート

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="customers">
    <html>
        <head>
            <h1>CUSTOMER REFERENCE</h1>
        </head>
        <body bgcolor="#DAF52C">
            <table border="1">
                <tr>
                    <th width="50">NAME</th>
                    <th width="50">INVOICE DATE</th>
                    <th width="50">PRODUCT UNIT</th>
                    <th width="50">ORDERED </th>
                    <th width="50">PRODUCT UNIT</th>
                    <th width="50">ORDERED </th>
                    <th width="50">INVOICE DATE</th>
                    <th width="50">PRODUCT UNIT</th>
                    <th width="50">ORDERED </th>
                    <th width="50">PRODUCT UNIT</th>
                    <th width="50">ORDERED </th>
                    <th width="50">INVOICE DATE</th>
                    <th width="50">PRODUCT UNIT</th>
                    <th width="50">ORDERED </th>
                    <th width="50">PRODUCT UNIT</th>
                    <th width="50">ORDERED </th>
                    <th width="50">INVOICE TOTAL</th>
                </tr>
                <xsl:for-each select="customer">
                    <tr>
                        <td><xsl:value-of select="clientname"/></td>
                        <xsl:for-each select="invoices">
                            <td><xsl:value-of select="invoiceDate"/></td>
                            <xsl:for-each select="product">
                                <td><xsl:value-of select="PriceUnit"/></td>
                                <td><xsl:value-of select="Ordered"/></td>
                            </xsl:for-each>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>
4

2 に答える 2

0

これは、この質問の最初の投稿に対するDimitreの回答に相当するXSLT1.0です。

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  xmlns:so="https://stackoverflow.com/questions/12338944"
  exclude-result-prefixes="xsl exsl so">
<xsl:output method="html" />
<xsl:strip-space elements="*" />

 <xsl:template match="/*">
  <html>
    <table border="1">
     <thead>
       <tr>
         <td>Name</td><td>Total</td>
       </tr>
     </thead>
     <xsl:apply-templates/>
    </table>
  </html>
 </xsl:template>

 <xsl:template match="customer">
  <tr>
    <td><xsl:value-of select="clientname"/></td>
    <td>
      <xsl:variable name="sales">
        <xsl:for-each select="invoices/product">
          <so:sale><xsl:value-of select="PriceUnit * Ordered"/></so:sale>
        </xsl:for-each>
      </xsl:variable>
      <xsl:value-of select="sum(exsl:node-set($sales)/so:sale)"/>
      </td>
  </tr>
 </xsl:template>
</xsl:stylesheet>

サンプルドキュメントでは、これにより出力が生成されます...

<html>
  <table border="1">
    <thead>
      <tr>
        <td>Name</td>
        <td>Total</td>
      </tr>
    </thead>
    <tr>
      <td>troy madison</td>
      <td>26589</td>
    </tr>
    <tr>
      <td>Morris</td>
      <td>635</td>
    </tr>
  </table>
</html>
于 2012-09-09T14:15:53.740 に答える
0

これは、拡張機能を必要としない純粋なXSLT1.0ソリューションです。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <html>
    <table border="1">
     <thead>
       <tr>
         <td>Name</td><td>Total</td>
       </tr>
     </thead>
     <xsl:apply-templates/>
    </table>
  </html>
 </xsl:template>

 <xsl:template match="customer">
  <tr>
    <td><xsl:value-of select="clientname"/></td>
    <td>
      <xsl:call-template name="sumProducts">
       <xsl:with-param name="pNodes" select="invoices/product"/>
       <xsl:with-param name="pName1" select="'PriceUnit'"/>
       <xsl:with-param name="pName2" select="'Ordered'"/>
      </xsl:call-template>
    </td>
  </tr>
 </xsl:template>

 <xsl:template name="sumProducts">
   <xsl:param name="pNodes"/>
   <xsl:param name="pName1"/>
   <xsl:param name="pName2"/>
   <xsl:param name="pAccum" select="0"/>

   <xsl:choose>
     <xsl:when test="not($pNodes)">
       <xsl:value-of select="$pAccum"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:call-template name="sumProducts">
        <xsl:with-param name="pNodes" select="$pNodes[position() >1]"/>
        <xsl:with-param name="pName1" select="$pName1"/>
        <xsl:with-param name="pName2" select="$pName2"/>
        <xsl:with-param name="pAccum" select=
        "$pAccum + $pNodes[1]/*[name()=$pName1] * $pNodes[1]/*[name()=$pName2]"/>
      </xsl:call-template>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

この変換が提供されたXMLドキュメントに適用される場合:

<customers>
    <customer>
        <clientname>troy madison</clientname>
        <invoices>
            <invoiceDate>8/8/98</invoiceDate>
            <product>
                <PriceUnit>1000</PriceUnit>
                <Ordered>2</Ordered>
            </product>
            <product>
                <PriceUnit>5400</PriceUnit>
                <Ordered>3</Ordered>
            </product>
        </invoices>
        <invoices>
            <invoiceDate>1/4/98</invoiceDate>
            <product>
                <PriceUnit>300</PriceUnit>
                <Ordered>4</Ordered>
            </product>
            <product>
                <PriceUnit>6000</PriceUnit>
                <Ordered>1</Ordered>
            </product>
        </invoices>
        <invoices>
            <invoiceDate>03/5/99</invoiceDate>
            <product>
                <PriceUnit>549</PriceUnit>
                <Ordered>1</Ordered>
            </product>
            <product>
                <PriceUnit>320</PriceUnit>
                <Ordered>2</Ordered>
            </product>
        </invoices>
    </customer>
    <customer>
        <clientname>Morris</clientname>
        <invoices>
            <invoiceDate>1/1/00</invoiceDate>
            <product>
                <PriceUnit>59</PriceUnit>
                <Ordered>3</Ordered>
            </product>
            <product>
                <PriceUnit>55</PriceUnit>
                <Ordered>1</Ordered>
            </product>
        </invoices>
        <invoices>
            <invoiceDate>11/1/01</invoiceDate>
            <product>
                <PriceUnit>10</PriceUnit>
                <Ordered>2</Ordered>
            </product>
            <product>
                <PriceUnit>54</PriceUnit>
                <Ordered>1</Ordered>
            </product>
        </invoices>
        <invoices>
            <invoiceDate>03/2/01</invoiceDate>
            <product>
                <PriceUnit>30</PriceUnit>
                <Ordered>1</Ordered>
            </product>
            <product>
                <PriceUnit>299</PriceUnit>
                <Ordered>1</Ordered>
            </product>
        </invoices>
    </customer>
</customers>

必要な正しい結果が生成されます:

<html>
   <table border="1">
      <thead>
         <tr>
            <td>Name</td>
            <td>Total</td>
         </tr>
      </thead>
      <tr>
         <td>troy madison</td>
         <td>26589</td>
      </tr>
      <tr>
         <td>Morris</td>
         <td>635</td>
      </tr>
   </table>
</html>

説明

  1. 再帰的に呼び出される名前付きテンプレート(sumProducts)。これは、子の積を合計する必要があるノードのノードセット($pNodes)と、値の積を合計する必要がある2つの子要素($pName1および$pName2)の名前をパラメーターとして受け取ります。初期値が0の最後の補助パラメーターがあります。これは$pAccum、各再帰呼び出しの間に累積された結果を格納するために使用されます。

  2. 停止条件は、渡されたpNodesノードセットが空の場合です。この場合、累積合計($pAccum)を出力します。

  3. 停止条件がfalseの場合、最初のノードが削除され、現在の製品が追加され$pNodesた新しい値がであると自分自身を呼び出します。$pAccum

注意してください

テンプレートは非常に一般的であり、sumProducts製品の合計が必要な場合に使用できます。

したがって、それは独自のスタイルシートファイルに配置する価値があり、製品の合計が必要なときはいつでも他のスタイルシートによってインポートされます。

于 2012-09-09T15:30:37.573 に答える