xsl 使用键相乘 [英] xsl multiply using a key

查看:14
本文介绍了xsl 使用键相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我不知道如何提问.我有 3 个 xml 文档.我有一个正在工作的 xsl,只是我不知道价格的总和乘以数量.我的时间不多了.请帮忙...这是 xsl ---

<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"><头><title>订单列表</title><link href="cust.css" rel="stylesheet" type="text/css"/><身体><div id="包裹"><h1><img src = "logo.png" alt="我的东西"/></h1><h2>我们的订单</h2><table id="orderTable"><tr>第<第>个订单号个<th>客户</th><th>Gr</th><th>产品</th><th>价格</th><th>数量</th><th>总成本</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><!-- 无法计算订单总数,需要从 products.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>

</html></xsl:模板></xsl:stylesheet>

--ord.xml 示例

---prod.xml

<product prodID="prod001"><prodGrp>铅笔</prodGrp><prodName>绿色</prodName><description>写得好</description><prodPrice>4.95</prodPrice><日期>2017-02-02</日期><图像><img src="pencil.jpg"/></图像></产品><product prodID="prod010"><prodGrp>pen</prodGrp><prodName>粗笔</prodName><description>易于抓握</description><prodPrice>.95</prodPrice><日期>2017-01-01</日期><图像><img src="pen.jpg"/></图像></产品></list>

需要 4 个订单时间 4.95 的产品.提前谢谢你...

解决方案

如果您使用的是 XSLT 2.0,那么您可以直接在另一个文档上使用 key,而无需像您现在所做的那样先将上下文切换到它.

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

XSLT 2.0

<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"><!-- 跳过--><表格边框=1"><tr>第<第>个订单号个<th>产品名称</th><th>价格</th><th>数量</th><th>总成本</th>第<第>个订单日期个</tr><xsl:for-each select="order"><tr><td><xsl:value-of select="@orderID"/></td><!-- 跳过--><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></xsl:模板></xsl:stylesheet>

使用您的输入示例,在 prod.xml 文件旁边,您应该看到如下结果:

Sorry, I don't know how to ask the question. I have 3 xml doc. I have an xsl that is working except for I can't figure out how the total of the price times the quantity. I'm running out of time. Please help... Here is the 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 example

<?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>

Need 4 from order times 4.95 from product. Thank you in advance...

解决方案

If you're using XSLT 2.0, then you can use key directly on another document without having to switch the context to it first, like you're doing now.

Here's a simplified example. This is assuming ord.xml is the input document for the XSLT transformation.

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>

Using your input example, alongside the prod.xml file, you should see a result like this:

这篇关于xsl 使用键相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆