473,387 Members | 1,572 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,387 software developers and data experts.

wrong element in transformed xml

xml data

<?xml version="1.0" ?>
<statement>
<accounts>
<account number="22">
<currency symbol="USD">
<transactions>
<transaction>
<id>1</id>
</transaction>
</transactions>
</currency>
<currency symbol="EUR">
<transactions>
<transaction>
<id>2</id>
</transaction>
</transactions>
</currency>
</account>
</accounts>
</statement>

transformed with msxml parser using stylesheet

<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" standalone="yes"/>
<xsl:template match="/">
<xsl:element name="VFPData">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="/statement/accounts/account/currency/transactions/*">
<xsl:element name="result">
<xsl:element name="id"><xsl:value-of select="id"/></xsl:element>
<xsl:element name="currency"><xsl:value-of
select="../../../currency/@symbol"/></xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
produces
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<VFPData><result>
<id>1</id>
<currency>USD</currency>
</result>
<result><id>2</id>
<currency>USD</currency>
</result></VFPData>
In this output currency element value for id 2 is wrong: it must be EUR

How to change stylesheet so that EUR is in id 2 record ?

Andrus.

Jul 12 '08 #1
2 1211
Off-topic, btw.

You are going back too many levels

At a guess (untested), try ="../../@symbol"

Note, however, that I prefer the "build upwards" approach - i.e. I'd
stop at "currency", and have something like:

<xsl:variable name="symbol" select="@symbol"/>

then I'd perform the downward matches, and I can use this in later
code, such as <xsl:value-of select="$symbol"/>. Note that xsl
variables are immutable within their declaring current scope.

Another thought: you're doing things the hard way. There is no need to
use <xsl:element/unless the name is unknown. Consider (untested, for
demonstratio only):

<xsl:template match="/">
<VFPData>
<xsl:apply-templates select="/statement/accounts/account/
currency">
</VFPData>
</xsl:template>

<xsl:template match="/statement/accounts/account/currency">
<xsl:variable name="symbol" select="@symbol"/>
<xsl:foreach select="transactions/transaction">
<result>
<id><xsl:value-of select="id"/></id>
<currency><xsl:value-of select="$symbol"/></currency>
</result>
</xsl:foreach>
</xsl:template>
Note that attributes are even easier; you don't need to use
xsl:attribute much - instead of:
<el><xsl:attribute name="foo"><xsl:value-of select="bar"/></
xsl:attribute></el>
you just need
<el foo="{bar}"/>

Marc
Jul 12 '08 #2
On Jul 12, 12:16*pm, Marc Gravell <marc.grav...@gmail.comwrote:
Off-topic, btw.

You are going back too many levels

At a guess (untested), try ="../../@symbol"
I would suggest simply "ancestor::currency/@symbol".
Note, however, that I prefer the "build upwards" approach - i.e. I'd
stop at "currency", and have something like:

<xsl:variable name="symbol" select="@symbol"/>

then I'd perform the downward matches, and I can use this in later
code, such as <xsl:value-of select="$symbol"/>. Note that xsl
variables are immutable within their declaring current scope.
xsl:for-each is traditionally frowned upon in XSLT code when a
template would do. Just how pragmatical it is, is, of course, a matter
of debate - but personally, I've dealt with a lot of stylesheets of
both kinds, and those that were heavy on templates were invariably
more maintainable than those who did the same with for-each. Perhaps
it's just my luck.
Jul 12 '08 #3

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

Similar topics

9
by: Joris Gillis | last post by:
Hi everyone, I was wondering... Can one element have multiple (identically named) attributes in XML? e.g. Is the following document valid XML? <?xml version="1.0" encoding="UTF-8"?> <root...
0
by: Ben Engbers | last post by:
Hi, This is the first time I use SAX in order to translate my XML and I encountered the following problem. I use the following Java-code to produce xml that should be transformed (Based on...
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
28
by: Andy Dingley | last post by:
What specifies the permitted root element(s) for a document ? HTML, SGML, XHTML or XML ? Valid HTML documents need to have a well-known DTD and a doctypedecl in each document like this:...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.