xml


xsl multiply using a key


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:

Related Links

Difference between XLS and XSLFO
Error while Convert xml to Object or Map in Scala using xstream
use awk to edit xml file
Why isnt my XML and XSLT Stylesheet not loading in Chrome?
XML of an online game apk
XSLT - Dynamic Map Columns
Suggestion for free XSLT tool [closed]
What does <tag/> mean in XML?
XSLT2.0 adding based on condition in current-group()
Validating XML in Spark Streaming and Kafka
Error message with SOAPUI
Insert value under specific node
do loop for structure check already exist or not
how to remove the xml elements with the help of the xsl?
How to generate dynamic XML metadata with CloverETL
How to insert dynamic formula in excel vba?

Categories

HOME
phpword
kentico
apache-jena
gpu
r-googlesheets
appcelerator
maxscript
frontend
boto3
computer-science
augeas
pega
onedrive-api
smartclient
guzzle
blade
string-concatenation
exit
resourcebundle
itunes
8051
testcomplete
alm
calculation
corenlp-server
send
sammy.js
dynamic-jasper
indy10
imx6
salesforce-communities
google-knowledge-graph
lint
sessionstorage
tex
modernizr
gdata
metawidget
unordered-map
xmldocument
optimizely
textsum
serde
robomongo
dtsearch
google-cse
flyout
taocp
xcode-server
segment-io
model-associations
x-tag
xdoclet
qiime
spreadjs
ikiwiki
halcon
jparsec
rasterize
iokit
morton-number
google-books
dock
rdflib
rapidsvn
webmatrix-3
cocos3d
teaspoon
machine-instruction
mutual-authentication
radiant
terminate
gae-quotas
koala
inappsettingskit
ui-select2
time.h
diem
pagedown
cascadingdropdown
device-width
joomla3.1
xcode3.2
iboutlet
pyunit
subgraph
database-create
fitbounds
google-floodlight
cgbitmapcontextcreate
event-receiver
photolibrary
objective-j
nstokenfield
feasibility
lobo-cobra
wordprocessingml
sifr3
dsoframer
lemmatization

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile