472,954 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 2101
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.