0

我在处理这个 xslt 时遇到了困难,这让我在总结收到的总收据时感到头疼是一张包含 2 个办公室和每个客户 2 种产品总价值的表格。我是 xml 和 xslt 的新手,请帮助

<offices>
  <office>
    <customerproduct>
      <Price>10</Price>
       <Stock>20</Stock>
       <Ordered>25</Ordered>
    </customerproduct>
    <customerproduct>
      <Price>10</Price>
       <Stock>2</Stock>
       <Ordered>15</Ordered>
    </customerproduct>
   <receiptno.>1</receiptno.>
  </office>
  <office>
    <customerproduct>
      <Price>14</Price>
       <Stock>20</Stock>
       <Ordered>24</Ordered>
    </customerproduct>
    <customerproduct>
      <Price>100</Price>
       <Stock>2</Stock>
       <Ordered>100</Ordered>
    </customerproduct>
  <receiptno.>2</receiptno.>
 </office>
</offices>
4

1 回答 1

0

这种转变

<xsl:stylesheet version="2.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">
     <xsl:apply-templates/>
    </table>
  </html>
 </xsl:template>

 <xsl:template match="office">
  <tr>
    <td><xsl:value-of select="concat('Office',position())"/></td>
    <td><xsl:value-of select="sum(*/(Price*Stock))"/></td>
  </tr>
 </xsl:template>
</xsl:stylesheet>

当应用于以下 XML 文档时(提供的文本是在更正几个畸形后提供的):

<offices>
    <office>
        <customerproduct>
            <Price>10</Price>
            <Stock>20</Stock>
            <Ordered>25</Ordered>
        </customerproduct>
        <customerproduct>
            <Price>10</Price>
            <Stock>2</Stock>
            <Ordered>15</Ordered>
        </customerproduct>
        <receiptno.>1</receiptno.>
    </office>
    <office>
        <customerproduct>
            <Price>14</Price>
            <Stock>20</Stock>
            <Ordered>24</Ordered>
        </customerproduct>
        <customerproduct>
            <Price>100</Price>
            <Stock>2</Stock>
            <Ordered>100</Ordered>
        </customerproduct>
        <receiptno.>2</receiptno.>
    </office>
</offices>

产生(我们可以猜测的是)想要的结果:

<html>
   <table border="1">
      <tr>
         <td>Office1</td>
         <td>220</td>
      </tr>
      <tr>
         <td>Office2</td>
         <td>480</td>
      </tr>
   </table>
</html>
于 2012-09-08T18:16:18.360 回答