在XSLT中使用msxslscript[转] (0分)

  • 主题发起人 主题发起人 netnice
  • 开始时间 开始时间
N

netnice

Unregistered / Unconfirmed
GUEST, unregistred user!
作者:rwpublic
email: rwpublic@263.net
日期:2001-4-18 15:59:53
invoice.xml
<?xml version="1.0" encoding="windows-1252"?>
<?xml-stylesheet type="text/xsl" href="invoice.xsl"?>
<invoices xmlns:dt="urn:schemas-microsoft-com:datatypes">
<customers>
<customer id="2513">Joe</customer>
<customer id="4871">Bob</customer>
<customer id="7806">Jane</customer>
<customer id="1579">Bruce</customer>
<customer id="2533">John</customer>
<customer id="1445">Bill</customer>
</customers>
<invoice id="5034">
<customer ref="2513"/>
<items>
<item>
<product-number>50014371</product-number>
<description>Windbreaker (teal)</description>
<qty dt:dt="number">1</qty>
<price dt:dt="number">18.00</price>
</item>
</items>
</invoice>
<invoice id="5035">
<customer ref="4871"/>
<items>
<item>
<product-number>61570910</product-number>
<description>Arctic Parka</description>
<qty dt:dt="number">1</qty>
<price dt:dt="number">160.00</price>
</item>
<item>
<product-number>ISBN:1-57231-686-1</product-number>
<description>Signal Flare 6-pack</description>
<qty dt:dt="number">3</qty>
<price dt:dt="number">48.50</price>
</item>
<item>
<product-number>68710047</product-number>
<description>Woolen Mittens</description>
<qty dt:dt="number">1</qty>
<price dt:dt="number">14.50</price>
</item>
</items>
</invoice>
<invoice id="5036">
<customer ref="7806"/>
<items>
<item>
<product-number>56812007</product-number>
<description>Tropical Shade Hat</description>
<qty dt:dt="number">1</qty>
<price dt:dt="number">29.90</price>
</item>
<item>
<product-number>74500231</product-number>
<description>Bug Repellant</description>
<qty dt:dt="number">3</qty>
<price dt:dt="number">4.95</price>
</item>
</items>
</invoice>
<invoice id="5037">
<customer ref="1579"/>
<items>
<item>
<product-number>65040097</product-number>
<description>Arctic Parka</description>
<qty dt:dt="number">2</qty>
<price dt:dt="number">160.00</price>
</item>
<item>
<product-number>2310078</product-number>
<description>Tundra Jacket</description>
<qty dt:dt="number">1</qty>
<price dt:dt="number">105.50</price>
</item>
</items>
</invoice>
<invoice id="5038">
<customer ref="2533"/>
<items>
<item>
<product-number>54807910</product-number>
<description>DVD Player</description>
<qty dt:dt="number">1</qty>
<price dt:dt="number">342</price>
</item>
<item>
<product-number>77842448</product-number>
<description>Oddesy Mousepad</description>
<qty dt:dt="number">1</qty>
<price dt:dt="number">15</price>
</item>
</items>
</invoice>
<invoice id="5039">
<customer ref="1445"/>
<items>
<item>
<product-number>65470025</product-number>
<description>Adventure Rations (Tofu)</description>
<qty dt:dt="number">20</qty>
<price dt:dt="number">6.00</price>
</item>
<item>
<product-number>65470025</product-number>
<description>Adventure Rations (Chicken)</description>
<qty dt:dt="number">20</qty>
<price dt:dt="number">6.00</price>
</item>
<item>
<product-number>24500358</product-number>
<description>Adventure Rations (Beef)</description>
<qty dt:dt="number">20</qty>
<price dt:dt="number">6.00</price>
</item>
</items>
</invoice>
</invoices>

invoice.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="mynamespace"
version="1.0">
<xsl:template match="/">
<HTML>
<STYLE>
TD {font-size:9pt}
</STYLE>
<BODY STYLE="font:9pt Verdana">
<H3>Invoices</H3>
<TABLE BORDER="1">
<TR>
<TD><B>Qty</B></TD>
<TD><B>Description</B></TD>
<TD><B>Price</B></TD>
<TD><B>Discount</B></TD>
<TD><B>Total</B></TD>
</TR>
<xsl:for-each select="invoices/invoice">
<xsl:variable name="customer-id" select="./customer/@ref"/>
<TR>
<TD COLSPAN="5" STYLE="border:none;
background-color:#DDDDDD">
Invoice #<xsl:value-of select="@id"/>,
for customer: <xsl:value-of select="/invoices/customers/customer[@id=$customer-id]"/>
</TD>
</TR>
<xsl:for-each select="items/item">
<TR>
<TD>
<xsl:value-of select="qty"/>
</TD>
<TD>
<xsl:value-of select="description"/>
</TD>
<TD>
$<xsl:value-of select="format-number(price,'#,##0.00')"/>
</TD>
<TD> <!-- 10% volume discount -->
<xsl:if test="qty[. &amp;gt;= 10]">$<xsl:value-of select="format-number(price * .10,'##0.00')"/></xsl:if>
</TD>
<TD STYLE="text-align:right"> <!-- line total -->
<xsl:choose>
<xsl:when test="qty[. &amp;gt;= 10]">
$<xsl:value-of select="format-number((price * .90) * qty,'#,##0.00')"/>
</xsl:when>
<xsl:otherwise>
$<xsl:value-of select="format-number(price * qty,'#,##0.00')"/>
</xsl:otherwise>
</xsl:choose>
</TD>
</TR>
</xsl:for-each>
<TR>
<TD COLSPAN="4"></TD>
<TD STYLE="text-align:right;
border:none;
border-top:1px solid black">$<xsl:value-of select="format-number(user:invoiceTotal(.),'#,##0.00')"/>
</TD>
</TR>
<TR/>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>

<msxsl:script implements-prefix="user"><![CDATA[
function invoiceTotal(e)
{
var invoice = e.nextNode();
var items = invoice.selectNodes("items/item");
var sum = 0;
for (var item = items.nextNode();
item;
item = items.nextNode())
{
var price = item.selectSingleNode("price").nodeTypedValue;
var qty = item.selectSingleNode("qty").nodeTypedValue;
if (qty >= 10)
price = 0.9*price;
sum += price * qty;
}
return sum;
}
]]></msxsl:script>
</xsl:stylesheet>
 
后退
顶部