473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to make such XSLT?

Hi,

I have small experience in XSLT processing and I have a problem which I
cannot solve. Can you look at it?

I have an input file containing info about bank accounts like this:

(...)
<acc id="1">
<balance>100</balance>
<currency>USD</currency>
</acc>
<acc id="2">
<balance>200</balance>
<currency>EUR</currency>
</acc>
(.... and so on ....)

now, additionaly i have a file with currency definitions:

(...)
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
(...)

what I need is to get such a result:

(...)
<acc id="1">
<balance>100</balance>
<currency-id>1</currency-id>
</acc>
<acc id="2">
<balance>200</balance>
<currency-id>2</currency-id>
</acc>
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
(...)

I need to get it by some XSLT program which has the first file (with
accounts) as input and must produce the last file. It can contain the
file with currency definitions (as this file is constant).
I imagine I need some kind of program where XSLT creates a map type
array from currency definitions and when processing accounts it replaces
currency codes with their respective ids.
I must add that I have many currencies (above 100 or sth) so it would be
easier no to define map array by hand, but I can do it if it is necessary.

Can you give me any hint or an address of any similar XSLT example? Thanks.

Maciek
Jul 20 '05 #1
8 2128
Maciej Wegorkiewicz wrote:
Hi,

I have small experience in XSLT processing and I have a problem which I
cannot solve. Can you look at it?

I have an input file containing info about bank accounts like this:

(...)
<acc id="1">
<balance>100</balance>
<currency>USD</currency>
</acc>
<acc id="2">
<balance>200</balance>
<currency>EUR</currency>
</acc>
(.... and so on ....)

now, additionaly i have a file with currency definitions:

(...)
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
(...)

what I need is to get such a result:

(...)
<acc id="1">
<balance>100</balance>
<currency-id>1</currency-id>
</acc>
<acc id="2">
<balance>200</balance>
<currency-id>2</currency-id>
</acc>
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
(...)

I need to get it by some XSLT program which has the first file (with
accounts) as input and must produce the last file. It can contain the
file with currency definitions (as this file is constant).
I imagine I need some kind of program where XSLT creates a map type
array from currency definitions and when processing accounts it replaces
currency codes with their respective ids.
I must add that I have many currencies (above 100 or sth) so it would be
easier no to define map array by hand, but I can do it if it is necessary.

Can you give me any hint or an address of any similar XSLT example? Thanks.

Maciek


Maybe it is better to put the currency id mappings in a separate
namespace than the acc-balance information.

Note you can put two files in a xslt-transformation and have one output.

The xslt wil look something like this:

<xsl:for-each select="ac">
<xsl:copy-of select=".">
<xsl:for-each select="@*">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:copy-of select="./balance">
</xsl:copy-of>
<currency-id>
<xsl:value-of select="mapping-namespace:currency[code=./currency]/@id"/>
</currency-id>
</xsl:copy-of>
</for-each>
I've not tested it so it could be invalid, but i hope it helpes you
Jul 20 '05 #2
Hi,

You can use the document() function in XSLT to bring in additional input XML
documents into the transformation.

For example, say your primary input XML (the accounts) looked like...

== accounts.xml =================================
<?xml version="1.0"?>
<accounts>
<acc id="1">
<balance>100</balance>
<currency>USD</currency>
</acc>
<acc id="2">
<balance>200</balance>
<currency>EUR</currency>
</acc>
</accounts>
== end of accounts.xml ==========================

and your secondary input XML document resided in the same directory as
"accounts.xml" and looked like...

== currencies.xml ===============================
<?xml version="1.0"?>
<currencies>
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
</currencies>
== end of currencies.xml ========================

then your XSLT might look something like...

== XSLT ===============================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:variable name="currency-lookups"
select="document('currencies.xml',/)/currencies/currency"/>

<xsl:template match="accounts">
<output>
<xsl:apply-templates select="acc"/>
<xsl:copy-of select="$currency-lookups"/>
</output>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@* | text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>

<xsl:template match="acc/currency">
<currency-id>
<xsl:value-of select="$currency-lookups[code = current()]/@id"/>
</currency-id>
</xsl:template>
</xsl:stylesheet>
== end XSLT ========================
HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
"Maciej Wegorkiewicz" <we******@interia.pl> wrote in message
news:cm**********@domitilla.aioe.org...
Hi,

I have small experience in XSLT processing and I have a problem which I
cannot solve. Can you look at it?

I have an input file containing info about bank accounts like this:

(...)
<acc id="1">
<balance>100</balance>
<currency>USD</currency>
</acc>
<acc id="2">
<balance>200</balance>
<currency>EUR</currency>
</acc>
(.... and so on ....)

now, additionaly i have a file with currency definitions:

(...)
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
(...)

what I need is to get such a result:

(...)
<acc id="1">
<balance>100</balance>
<currency-id>1</currency-id>
</acc>
<acc id="2">
<balance>200</balance>
<currency-id>2</currency-id>
</acc>
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
(...)

I need to get it by some XSLT program which has the first file (with
accounts) as input and must produce the last file. It can contain the
file with currency definitions (as this file is constant).
I imagine I need some kind of program where XSLT creates a map type
array from currency definitions and when processing accounts it replaces
currency codes with their respective ids.
I must add that I have many currencies (above 100 or sth) so it would be
easier no to define map array by hand, but I can do it if it is necessary.

Can you give me any hint or an address of any similar XSLT example? Thanks.
Maciek

Jul 20 '05 #3
Thanks, it works great,

But I have one question, sorry if I seem to be bothering.
How can I include currencies file into XSLT program? I mean something like:

<xsl:variable name="currency-lookups">
<currencies>
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
</currencies>
</xsl:variable>

When I put what is above, I get message that I cannot use result tree
fragment in XPath expression. How to distinct tree fragment from what is
initialized in such variable expression:

<xsl:variable name="currency-lookups"
select="document('currencies.xml',/)/currencies/currency"/>

It seems to be some kind of map array (lookup array) or something, not
the tree fragment.

So, how can I modify constant tree fragment to become such an array?

Maciek

Jul 20 '05 #4


Maciej Wegorkiewicz wrote:

How can I include currencies file into XSLT program? I mean something like:

<xsl:variable name="currency-lookups">
<currencies>
<currency id="1">
<code>USD</code>
</currency>
<currency id="2">
<code>EUR</code>
</currency>
</currencies>
</xsl:variable>

When I put what is above, I get message that I cannot use result tree
fragment in XPath expression.


Within XSLT 1.0 it is indeed not possible to use the contents of such a
variable in an XPath expression as the contents is not a nodeset but a
result tree fragment. Most XSLT processors however provide an extension
function to convert a result tree fragment into a nodeset, check the
documentation of your processor.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #5
and your secondary input XML document resided in the same directory as
"accounts.xml" and looked like...


Sorry for being tiring, but I am trying to resolve the problem by myself
and cannot do it, being complete newbie it seems...

Unfortunately my currency.xml input file does not reside in the same
directory as accounts.xml. Even more - it can reside in directory
completely not related to accounts.xml so I cannot hardcode the path in
xsl file. Is there any way to provide the file with parameter that can
contain the path?

Maciek
Jul 20 '05 #6


Maciej Wegorkiewicz wrote:
and your secondary input XML document resided in the same directory as
"accounts.xml" and looked like...

Unfortunately my currency.xml input file does not reside in the same
directory as accounts.xml. Even more - it can reside in directory
completely not related to accounts.xml so I cannot hardcode the path in
xsl file. Is there any way to provide the file with parameter that can
contain the path?


Well you could make sure it is available on a web server, either locally
e.g.
document('http://localhost/someDir/currency.xml')
or on the web e.g.
document('http://example.com/someDir/currency.xml')
that way the XSLT processor can find it.
Or use a parameter e.g.
<xsl:param name="currencyURL" />
that you set before the transformation as needed:
document($currencyURL)

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #7
> Or use a parameter e.g. <xsl:param name="currencyURL" /> that you set
before the transformation as needed: document($currencyURL)


How can I set the parameter from Java program?
I use javax.xml.Transformer class.
There I have transformer.setParameter() method.

When I set the parameter like that:

<xsl:param name="prm1">
http://www.eportfel.com/defjedn17.xml
</xsl:param>

document($prm1) works

When I do it like that:

<xsl:param name="prm1" select="defjednpath"/>

and
transformer.setParameter("defjednpath","http://www.eportfel.com/defjedn17.xml")
before transformation run
it does not work.

Maciek

PS. Can you give me any link on the web that tutorials / describes above
issues? I do not want to make this news thread a neverending story.
Jul 20 '05 #8
> <xsl:param name="prm1" select="defjednpath"/>

and
transformer.setParameter("defjednpath","http://www.eportfel.com/defjedn17.xml")
before transformation run
it does not work.


Ok, I figured it out. It should be:

<xsl:param name="prm1"/>

and

transformer.setParameter("prm1","http://www.eportfel.com/defjedn17.xml")

Maciek
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ted | last post by:
Was wondering if XSLT alone is appropriate for the following situation. From XML, I'm creating a small website (around 50 pages) with pages that link to each other through a nav menu and a...
2
by: Tom Corcoran | last post by:
I am working to ease updating of a html page by transforming 2 xml files. I was going to use xslt for this and had bought 2 unopened books, wrox xslt and o'reilly's xslt cookbook. But am now...
1
by: Mohit | last post by:
Hi Friends I have to call 1 of the 2 child XSLT files from the Main XSLT file based on some criteria. I want one child XSLT file will be executed by version 1 of XSLT processor and the other by...
2
by: jaydog | last post by:
Hello... I'm new to XSL, and I've written a XSLT file that converts a XML file to HTML format. When viewed in a browser, it appears exactly as I would like. However, if I want to look at the...
3
by: Teksure | last post by:
Hi group, searching in the Internet I found two products for XML which incorporate a very robust debugger for XSL/XSLT, I would like you to see these products and then, give me your opinion about...
1
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
12
by: Chris | last post by:
Hi, Just wondering if anyone out there knows if it is possible to convert a CSV to xml using XSLT? I've seen a lot of examples of xml to CSV, but is it possible to go back the other way? I...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.