[XSLT] Can not raise some information from a XML sub-node

F

for.fun

Hi everybody,

I have to transform an input XML into an output XML using a XSLT style
sheet.
On the input side, the information is stored into sub-nodes.
On the output side, I need to transform the country codes into country
names and report any transform error.
The only requirement is the error must be reported at the root level
("foo123" will cause the error in my following sample)

In order to do it, I use "template match" instructions.
When I am inside the "<code>foo123<code>" block, I did not find any way
to save the error or to raise it to the output root token.

You know that XSLT variables have only a local scope and can only be
instancied once. So I can not use them to save an error status and use
it later.

The brute-force issue would be to prune all the XML tree at the end of
the processus only to check for errors and then report them at the root
level but I am sure there is a better way to do it.


Can anyone help me ?


Thanks in advance.


----- XML samples -----

In the following sample, the error is due to "foo123" which is not a
country code.

* INPUT XML *

<root>
<group>
<country>
<code>44</code>
</country>
<country>
<code>33<code>
</country>
<country>
<code>foo123<code>
</country>
<group>
</root>


* OUTPUT XML *

<root>
<errcode>TRANSFORM_ERROR<errcode>
<countrynames>
<name>ENGLAND</name> <!-- code 44 -->
<name>FRANCE</name> <!-- code 33 -->
</countrynames>
</root>
 
P

Peter Flynn

Hi everybody,

I have to transform an input XML into an output XML using a XSLT style
sheet.
On the input side, the information is stored into sub-nodes.
On the output side, I need to transform the country codes into country
names and report any transform error.
The only requirement is the error must be reported at the root level
("foo123" will cause the error in my following sample)

In order to do it, I use "template match" instructions.
When I am inside the "<code>foo123<code>" block, I did not find any
way to save the error or to raise it to the output root token.

You know that XSLT variables have only a local scope and can only be
instancied once. So I can not use them to save an error status and use
it later.

The brute-force issue would be to prune all the XML tree at the end of
the processus only to check for errors and then report them at the
root level but I am sure there is a better way to do it.

This is one of those occasions when xsl:for-each is the right tool.
----- XML samples -----

In the following sample, the error is due to "foo123" which is not a
country code.

* INPUT XML *

<root>
<group>
<country>
<code>44</code>
</country>
<country>
<code>33<code>
</country>
<country>
<code>foo123<code>
</country>
<group>
</root>

<xsl:template match="root">
<xsl:for-each select="descendant::code">
<xsl:if test="your_test_for_code_values">
<errcode>
<xsl:text>Invalid code: </xsl:text>
<xsl:value-of select="."/>
</errcode>
</xsl:if>
</xsl:for-each>
<root>
<errcode>TRANSFORM_ERROR<errcode>
<countrynames>
<name>ENGLAND</name> <!-- code 44 -->
<name>FRANCE</name> <!-- code 33 -->
</countrynames>
</root>

///Peter
 
F

for.fun

Peter Flynn a écrit :

Thanks of lot.
It works fine and fast.
This is all what I expected !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top