xsl使用密钥相乘

时间:2017-05-08 00:53:56

标签: xml xslt

抱歉,我不知道如何提问。我有3个xml doc。我有一个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.95。提前谢谢你......

1 个答案:

答案 0 :(得分: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