XML to (simple) custom defined output

D

Duncan Bloem

Hello,

I am not a stylesheet designer, but I am wondering if (and how) the
following XML file can be transformed to a custom defined text output file.

from XML file:
<?xml version="1.0" encoding="UTF-8"?>
<BatchList>
<Header>
<SYSCODE>IOPL</SYSCODE>
<TRANDAT>020101</TRANDAT>
<WERKMY>45250</WERKMY>
<VALUTA>EUR</VALUTA>
</Header>
<Detail>
<SYSCODE>IOPL</SYSCODE>
<CODE>12547</CODE>
<LABEL1>1</LABEL1>
<BEDRA1>12</BEDRA1>
<BEDRB1>0</BEDRB1>
</Detail>
<Header>
<SYSCODE>IOPL</SYSCODE>
<TRANDAT>020101</TRANDAT>
<WERKMY>45250</WERKMY>
<VALUTA>USD</VALUTA>
</Header>
<Detail>
<SYSCODE>IOPL</SYSCODE>
<CODE>65478</CODE>
<LABEL1>1</LABEL1>
<BEDRA1>12</BEDRA1>
<BEDRB1>10</BEDRB1>
</Detail>
<Detail>
<SYSCODE>IOPL</SYSCODE>
<CODE>74784</CODE>
<LABEL1>2</LABEL1>
<BEDRA1>11</BEDRA1>
<BEDRB1>9</BEDRB1>
</Detail>
</BatchList>

To custom text file:
IOPL02010145250EUR
IOPL12547 1 12
IOPL02010145250USD
IOPL65478 1 12 10
IOPL74784 2 11 9

Any suggestions?
Thanks in advance.

Duncan
 
M

Martin Boehm

Duncan Bloem said:
Hello,

I am not a stylesheet designer, but I am wondering if (and how) the
following XML file can be transformed to a custom defined text output
file.

[...snip...]

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="text"/>

<xsl:template match="Header">
<xsl:value-of select="concat(SYSCODE, TRANDAT, WERKMY, VALUTA,
'
')"/>
</xsl:template>

<xsl:template match="Detail">
<xsl:variable name="label1">
<xsl:call-template name="padd">
<xsl:with-param name="len" select="number('5') -
string-length(LABEL1)"/>
<xsl:with-param name="str" select="LABEL1"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="bedra1">
<xsl:call-template name="padd">
<xsl:with-param name="len" select="number('6') -
string-length(BEDRA1)"/>
<xsl:with-param name="str" select="BEDRA1"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="bedrb1">
<xsl:call-template name="padd">
<xsl:with-param name="len" select="number('6') -
string-length(BEDRB1)"/>
<xsl:with-param name="str" select="BEDRB1"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat(SYSCODE, CODE, $label1, $bedra1, $bedrb1,
'
')"/>
</xsl:template>

<xsl:template name="padd">
<xsl:param name="len"/>
<xsl:param name="str"/>
<xsl:if test="number($str != 0)">
<xsl:choose>
<xsl:when test="$len &gt; 0">
<xsl:call-template name="padd">
<xsl:with-param name="len" select="$len - 1"/>
<xsl:with-param name="str" select="concat(' ', $str)"/>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="$str"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

The template for "Detail" elements could be somewhat more elegant, but I
cannot think of anything better right now. :)

The Result produced is:
IOPL02010145250EUR
IOPL12547 1 12
IOPL02010145250USD
IOPL65478 1 12 10
IOPL74784 2 11 9

That matches your wanted result, I consider your last line a typo.
IOPL02010145250EUR
IOPL12547 1 12
IOPL02010145250USD
IOPL65478 1 12 10
IOPL74784 2 11 9

I don't know if '
' (LF) suits you as newline, take '
'
(CRLF) if you want.

Martin
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top