Re: [xsl] Collect all the truncated fields in an array

Subject: Re: [xsl] Collect all the truncated fields in an array
From: David Mitchell <david.mitchell@xxxxxxxxx>
Date: Tue, 5 Oct 2010 21:56:39 -0500
Sure.

You need to stop thinking of storing away the results of your
computations as you go.

Instead, think of processing the input twice.

Once to create the structure you have so far (but truncate the values
as you go).

Then, process the input again and create the <TruncatedValues> element
and its content.

You need to read up on template modes.

If you are having these sorts of problems, you may want to invest in a
good book. XSLT Quickly is short and to the point. Any of Jeni
Tennison's books would be excellent.

On Tue, Oct 5, 2010 at 6:40 PM, sudheshna iyer <sudheshnaiyer@xxxxxxxxx>
wrote:
> Team,
>
> Thank you for helping. It worked. Now I want to extend the functionality. I
> changed the subject appropriately..
>
> XSL:
>
> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
> <xsl:template match="InputXml">
> <xsl:variable name="MAX_InputXmlValue1">1</xsl:variable>
> <xsl:variable name="MAX_InputXmlValue2">2</xsl:variable>
> <xsl:variable name="MAX_InputXmlValue3">3</xsl:variable>
> <OutputXml>
> <xsl:choose>
> <xsl:when test="InputXmlValue3='' and InputXmlValue2=''">
>    <outputTempVar1></outputTempVar1>
>    <outputVar1><xsl:value-of
> select="substring(InputXmlValue1,1,$MAX_InputXmlValue1)"/></outputVar1>
>    <outputVar2></outputVar2>
> </xsl:when>
> <xsl:when test="InputXmlValue3=''">
>    <outputTempVar1></outputTempVar1>
>    <outputVar1><xsl:value-of
> select="substring(InputXmlValue1,1,$MAX_InputXmlValue1)"/></outputVar1>
>    <outputVar2><xsl:value-of
> select="substring(InputXmlValue2,1,$MAX_InputXmlValue2)"/></outputVar2>
> </xsl:when>
> <xsl:otherwise>
>    <outputTempVar1><xsl:value-of
>
select="substring($InputXmlValue1,1,$MAX_InputXmlValue1)InputXmlValue1"/></ou
tputTempVar1>
>
>    <outputVar1><xsl:value-of
> select="substring(InputXmlValue2,1,$MAX_InputXmlValue2)"/></outputVar1>
>    <outputVar2><xsl:value-of
> select="substring(InputXmlValue3,1,$MAX_InputXmlValue3)"/></outputVar2>
> </xsl:otherwise>
> </xsl:choose>
> </OutputXml>
> </xsl:template>
> </xsl:stylesheet>
>
> In the above xsl, I am taking 3 input values and using the below logic,
mapped
> to 3 output values. During the mapping, I introduced truncation logic.
> Now I need to collect all the input values that got truncated at the end of
the
> program in the form of string array. Ultimate goal is that
> I need to frame an email collecting all truncated values.
>
> Conditions:
> 1. If all the 3 input variables are present (element present with no
blanks),
> map /InputXmlValue1 -> outputTempVar1, map /InputXmlValue2 -> outputVar1 and
map
> /InputXmlValue3 -> outputVar2.
> 2. If there are only two input variables present (/InputXmlValue1,
> /InputXmlValue2), map /InputXmlValue1 -> outputVar1 and map /InputXmlValue2
->
> outputVar2.
> 3. If there is only one input variable is present (//InputXmlValue1), map
> /InputXmlValue1 -> outputVar1.
>
>
> eg: If I send the following input xml
> INPUT:
> <?xml version="1.0" encoding="UTF-8"?>
> <InputXml>
>    <InputXmlValue1>12</InputXmlValue1>
>    <InputXmlValue2>345</InputXmlValue2>
>    <InputXmlValue3>678</InputXmlValue3>
> </InputXml>
>
> Based on the above xsl, output will be
> OUTPUT:
> <?xml version="1.0" encoding="UTF-8"?>
> <OutputXml>
>    <outputTempVar1>1</outputTempVar1>  ==> Truncated. "12" got truncated to
"1"
>    <outputVar1>34</outputVar1>    ==> Truncated. "345" got truncated to
"34"
>    <outputVar2>678</outputVar2>
> </OutputXml>
>
> What I want is:
> <?xml version="1.0" encoding="UTF-8"?>
> <OutputXml>
>    <outputTempVar1>1</outputTempVar1>  ==> Truncated. "12" got truncated to
"1"
>    <outputVar1>34</outputVar1>    ==> Truncated. "345" got truncated to
"34"
>    <outputVar2>678</outputVar2>
>    <TruncatedValues>'[InputXmlValue1: Initial[12],
> Truncated[1]],[InputXmlValue2: Initial[345],
Truncated[34]]</TruncatedValues>
> </OutputXml>
>
> Is this possible?
>
>
>
> ----- Original Message ----
> From: sudheshna iyer <sudheshnaiyer@xxxxxxxxx>
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Sent: Tue, October 5, 2010 11:18:45 AM
> Subject: Re: [xsl] Variable value change based on condition
>
> Thank you all. Let me try this.
>
>
>
> --- On Tue, 10/5/10, David Carlisle <davidc@xxxxxxxxx> wrote:
>
>> From: David Carlisle <davidc@xxxxxxxxx>
>> Subject: Re: [xsl] Variable value change based on condition
>> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
>> Cc: "sudheshna iyer" <sudheshnaiyer@xxxxxxxxx>
>> Date: Tuesday, October 5, 2010, 10:51 AM
>> On 05/10/2010 15:38, sudheshna iyer
>> wrote:
>> > Team, thank you for your patience. I complete
>> understand that converting java code to xslt doesn't make
>> much sense. But tech change required this, which I don't
>> have control on. Since I came from java background, it is
>> becoming harder for me..
>>
>> so there were no variables at all, you meant "elements" I
>> doubt anyone would have guessed that.
>>
>>
>> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>>
>> <xsl:template match="InputXml">
>> <OutputXml>
>> <xsl:choose>
>> <xsl:when test="InputXmlValue3='' and
>> InputXmlValue2=''">
>>
>> <outputTempVar1></outputTempVar1>
>>     <outputVar1><xsl:value-of
>> select="InputXmlValue1"/></outputVar1>
>>     <outputVar2></outputVar2>
>> </xsl:when>
>> <xsl:when test="InputXmlValue3=''">
>>
>> <outputTempVar1></outputTempVar1>
>>     <outputVar1><xsl:value-of
>> select="InputXmlValue1"/></outputVar1>
>>     <outputVar2><xsl:value-of
>> select="InputXmlValue2"/></outputVar2>
>> </xsl:when>
>> <xsl:otherwise>
>>     <outputTempVar1><xsl:value-of
>> select="InputXmlValue1"/></outputTempVar1>
>>     <outputVar1><xsl:value-of
>> select="InputXmlValue2"/></outputVar1>
>>     <outputVar2><xsl:value-of
>> select="InputXmlValue3"/></outputVar2>
>> </xsl:otherwise>
>> </xsl:choose>
>> </OutputXml>
>> </xsl:template>
>> </xsl:stylesheet>
>>
>>
>> ________________________________________________________________________
>> The Numerical Algorithms Group Ltd is a company registered
>> in England
>> and Wales with company number 1249803. The registered
>> office is:
>> Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United
>> Kingdom.
>>
>> This e-mail has been scanned for all viruses by Star. The
>> service is
>> powered by MessageLabs.
>> ________________________________________________________________________

Current Thread