2017-05-08 35 views
0

对不起,我不知道如何提问。我有3个XML文件。我有一个xsl正在工作,除了我无法弄清楚价格的总和是如何计算数量的。我没时间了。请帮助... 这里是xsl ---xsl乘以一个键

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
<xsl:output method="html" doctype-system="about:legacy-compat" encoding="UTF-8" indent="yes" /> 
    <xsl:variable name = "custIn" select = "document('cust.xml')" /> 
    <xsl:variable name = "prodIn" select = "document('prod.xml')" /> 
    <xsl:key name="custN" match="customer" use="@custID" /> 
    <xsl:key name="prodN" match="product" use="@prodID" /> 
    <xsl:template match="/orders"> 
     <html> 
      <head> 
       <title>Order List</title> 
       <link href="cust.css" rel="stylesheet" type="text/css" /> 
      </head> 

      <body> 
       <div id="wrap"> 
        <h1> 
         <img src = "logo.png" alt="My Things" /></h1> 
        <h2>Our Orders</h2> 
        <table id="orderTable"> 
         <tr> 
          <th>Order Num</th> 
          <th>Cust</th> 
          <th>Gr</th>       
          <th>Product</th> 
          <th>Price</th> 
          <th>Qty</th>        
          <th>Total Cost</th> 
          <th>Order Date</th> 
         </tr> 
         <xsl:for-each select = "order"> 
          <xsl:sort select="orderDate" /> 
          <tr> 
           <td><xsl:value-of select="@orderID" /></td> 
           <td> 
            <xsl:variable name="cID" select="@custID" /> 
            <xsl:for-each select="$custIn"> 
             <xsl:value-of select = "key('custN', $cID)/concat(first_name, ' ', last_name)" /> 
            </xsl:for-each></td> 
           <td><xsl:value-of select="prodGrp"/></td> 
           <xsl:variable name="pID" select="@prodID" /> 
            <xsl:for-each select="$prodIn"> 
             <td><xsl:value-of select="key('prodN', $pID)/prodName" /></td> 
             <td><xsl:value-of select="key('prodN', $pID)/format-number(prodPrice, '$###,##0.00')" /></td>        
            </xsl:for-each>         
           <td><xsl:value-of select="prodQty"/></td> 

<!-- can't calculate the total for the orders, need the price for each from the products.xml 
--can bring in the price each her <xsl:value-of select="key('prodN', $pID)/format-number(prodPrice, '$###,##0.00')" 
but has to be multiplied by the prodQty from the orders.xml -->         
           <td><xsl:value-of select="format-number(prodQty * 5, '$###,##0.00')" /> 
           </td> 

           <td><xsl:value-of select="format-date(orderDate,'[M01]/[D01]/[Y0001]')" /></td> 
          </tr> 
         </xsl:for-each>    
        </table> 
       </div> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

--ord.xml例如

<?xml version="1.0" encoding="UTF-8"?> 
<orders> 
    <order orderID = "ord10007" custID = "cust100030" prodID = "prod001" > 
     <orderDate>2017-02-02</orderDate> 
     <prodGrp>pencil</prodGrp> 
     <prodQty>6</prodQty> 
    </order> 
    <order orderID = "ord10020" custID = "cust100031" prodID = "prod010" > 
     <orderDate>2017-03-03</orderDate> 
     <prodGrp>pen</prodGrp> 
     <prodQty>4</prodQty>  
    </order> 
    <order orderID = "ord10050" custID = "cust10030" prodID = "prod010" > 
     <orderDate>2017-04-04</orderDate> 
     <prodGrp>pen</prodGrp> 
     <prodQty>7</prodQty> 
    </order> 
<orders> 

--- prod.xml

<?xml version="1.0" encoding="UTF-8"?> 
<list> 
    <product prodID="prod001"> 
     <prodGrp>pencil</prodGrp> 
     <prodName>green</prodName> 
     <description>write nice</description> 
     <prodPrice>4.95</prodPrice> 
     <date>2017-02-02</date> 
     <images> 
      <img src="pencil.jpg"/> 
     </images> 
    </product> 
    <product prodID="prod010"> 
     <prodGrp>pen</prodGrp> 
     <prodName>thick pen</prodName> 
     <description>easy to grip</description> 
     <prodPrice>.95</prodPrice> 
     <date>2017-01-01</date> 
     <images> 
      <img src="pen.jpg"/> 
     </images> 
    </product> 
</list> 

需要4个从订单从产品4.95倍。预先感谢您...

+0

你真的使用支持XSLT处理器2.0? –

+0

我正在使用java net.sf.saxon命令提示符变换 – LC0513

回答

1

如果您使用的是XSLT 2.0,那么您可以直接在另一个文档上使用密钥,而不必先切换上下文,就像现在所做的那样。

这是一个简化的例子。假设ord.xml是XSLT转换的输入文档。

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html"/> 

<xsl:param name="products" select="document('prod.xml')"/> 
<xsl:key name="product-by-id" match="product" use="@prodID" /> 

<xsl:template match="/orders"> 
    <!-- skipped --> 
    <table border="1"> 
     <tr> 
      <th>Order Num</th> 
      <th>Product Name</th> 
      <th>Price</th> 
      <th>Qty</th>        
      <th>Total Cost</th> 
      <th>Order Date</th> 
     </tr> 
     <xsl:for-each select="order"> 
      <tr> 
       <td> 
        <xsl:value-of select="@orderID" /> 
       </td> 
       <!-- skipped --> 
       <xsl:variable name="product" select="key('product-by-id', @prodID, $products)" /> 
       <td> 
        <xsl:value-of select="$product/prodName" /> 
       </td> 
       <td> 
        <xsl:value-of select="format-number($product/prodPrice, '$#,##0.00')" /> 
       </td> 
       <td> 
        <xsl:value-of select="prodQty" /> 
       </td> 
       <td> 
        <xsl:value-of select="format-number($product/prodPrice * prodQty, '$#,##0.00')" /> 
       </td> 
       <td> 
        <xsl:value-of select="format-date(orderDate,'[M01]/[D01]/[Y0001]')" /> 
       </td> 
      </tr> 
     </xsl:for-each>    
    </table> 
</xsl:template> 

</xsl:stylesheet> 

使用您的输入例如,沿着prod.xml文件,你会看到这样的结果:

enter image description here

+0

这个技巧。效果很好。欣赏它。 – LC0513

+0

难以置信当你疲倦时,脑中有多少死亡空间。有人可以帮我弄清楚如何1.创建一个计算小计价格和所有订单总数的xsl,然后在示例中的xsl中使用该xsl来计算每个客户的总成本(小计)和总成本对于订单xml中的所有客户? – LC0513

+0

我建议你发布一个新问题,并确保包含客户文件以及确切的预期结果。 –