By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,567 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,567 IT Pros & Developers. It's quick & easy.

wrong element in transformed xml

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.