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

Discussion in 'XML' started by for.fun@laposte.net, Dec 6, 2005.

  1. Guest

    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>
     
    , Dec 6, 2005
    #1
    1. Advertising

  2. Peter Flynn Guest

    wrote:

    > 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>
    </xsl:template>

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


    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/
     
    Peter Flynn, Dec 6, 2005
    #2
    1. Advertising

  3. Guest

    Re: Can not raise some information from a XML sub-node

    Peter Flynn a écrit :

    Thanks of lot.
    It works fine and fast.
    This is all what I expected !
     
    , Dec 8, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tjerk Wolterink
    Replies:
    2
    Views:
    1,437
    Dimitre Novatchev
    Aug 24, 2006
  2. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    408
    Gabriel Genellina
    Feb 5, 2007
  3. Ben
    Replies:
    2
    Views:
    898
  4. ernest
    Replies:
    2
    Views:
    292
    Roy Smith
    Nov 14, 2010
  5. Jack Bates
    Replies:
    0
    Views:
    280
    Jack Bates
    May 2, 2011
Loading...

Share This Page