including data from a separate xml-file

Here should go questions about transforming XML with XSLT and FOP.
jsail
Posts: 4
Joined: Thu Dec 25, 2008 7:00 pm

including data from a separate xml-file

Post by jsail »

A external application is sending error-xml-files as respond.xml, if an error occures. I try to transform these Messages into a readeble text. But the respond.xml has only error-codes. But I would like to see the real error-message, wich I can find in errors.xml
Is it possible to combine my xslt to errors.xml?

How could I include that errors.mxl to my xslt-File rtest.xsl?

Thank you for your help!

Joseph

A respond.xml looks abot like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<invoice:response xmlns:invoice="abc">
<invoice:invoice invoice_id="9549">
</invoice:invoice>
<invoice:status>
<invoice:rejected>
<invoice:error error="101"/>
<invoice:error error="103"/>
</invoice:rejected>
</invoice:status>
</invoice:response>
The errors.xml is like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<errors>
<error_id>101</error_id>
<message>invalid address</message>
<error_id>102</error_id>
<message>invalid amount</message>
<error_id>103</error_id>
<message>wrong currency</message>
</errors>
And my xslt-File looks this way:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="invoice:status" xmlns:invoice="abc">
<xsl:if test="invoice:rejected"> Responsetyp: Rejected

<xsl:for-each
select="invoice:rejected/invoice:error"> Error: <xsl:value-of select="@error"/>
<!-- Here should be the error-message from errors.xml-->
</xsl:for-each>

</xsl:if>
</xsl:template>
</xsl:stylesheet>
jsail
Posts: 4
Joined: Thu Dec 25, 2008 7:00 pm

Re: including data from a separate xml-file

Post by jsail »

I have found a solution.
First I had to separate the errors in errors.xml:
<?xml version="1.0" encoding="UTF-8"?>

Code: Select all

<response>
<errors>
<message_id>101</message_id>
<message>invalid address</message>
</errors>
<errors>
<message_id>102</message_id>
<message>invalid amount</message>
</errors>
<errors>
<message_id>103</message_id>
<message>wrong currency</message>
</errors>
</response>
Then in the XSLT I linked the errors.xml:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="invoice:status" xmlns:invoice="abc">
<xsl:if test="invoice:rejected"> Responsetyp: Rejected

<xsl:for-each
select="invoice:rejected/invoice:error">
<xsl:variable name="v_error_id" select="@error"/>
Error_id: <xsl:value-of select="$v_error_id" />
Error: <xsl:value-of select="document('errors.xml')/response/errors[message_id=$v_error_id]/message"/>
</xsl:for-each>

</xsl:if>
</xsl:template>
</xsl:stylesheet>
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: including data from a separate xml-file

Post by george »

Yes, you can use the document function to access data from another file. Note however that you do not necessary need to change the structure of the errors file as you can access the message using the following sibling axis, see for example

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="invoice:status" xmlns:invoice="abc">
<xsl:if test="invoice:rejected"> Responsetyp: Rejected
<xsl:for-each
select="invoice:rejected/invoice:error">
<xsl:text>&#10;Error: </xsl:text>
<xsl:value-of select="@error"/>
<!-- Here should be the error-message from errors.xml-->
<xsl:text> </xsl:text>
<xsl:value-of
select="document('errors.xml')/errors/error_id[.=current()/@error]/following-sibling::message[1]"/>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George
George Cristian Bina
jsail
Posts: 4
Joined: Thu Dec 25, 2008 7:00 pm

Re: including data from a separate xml-file

Post by jsail »

Hello George,

your way ist even simpler to use than my idea.
I included it now in my App.

Thank you for your help and best wishes for 2009

Joseph
Post Reply