Rafa Vieira
Rafa Vieira

Reputation: 45

XSLT field validation

I have a requirement to validate a field content which is within a 1..N structure, so the data come in pairs of RESULT_ID and RESULT_VALUE.

If "<" is found in Result.Min_Limit characteristic, its value must be put in the Max_Limit characteristic target field and Min_Limit must be cleared out in target structure.

Sample of source structure with data:

<?xml version="1.0" encoding="UTF-8"?>
<TEST>
    <RESULTS>
           <RESULT>
                <D_RESULT>
                 <D_RESULT_ID>Result.Derived</D_RESULT_ID>
                 <D_RESULT_VALUE>59W</D_RESULT_VALUE>
            </D_RESULT>
            <D_RESULT>
                 <D_RESULT_ID>Result.Min_Limit</D_RESULT_ID>
                 <D_RESULT_VALUE>&lt;=600.0000</D_RESULT_VALUE>
            </D_RESULT>
            <D_RESULT>
                 <D_RESULT_ID>Result.Max_Limit</D_RESULT_ID>
                 <D_RESULT_VALUE/>
            </D_RESULT>
            <D_RESULT>
                 <D_RESULT_ID>Result.FailedCriticalLvl</D_RESULT_ID>
                 <D_RESULT_VALUE>false</D_RESULT_VALUE>
            </D_RESULT>
       </RESULT>
  </RESULTS>
</TEST>

Expected target structure should be as follow:

<?xml version="1.0" encoding="UTF-8"?>
<TEST>
  <RESULTS>
       <RESULT>
            <D_RESULT>
                 <D_RESULT_ID>Result.Derived</D_RESULT_ID>
                 <D_RESULT_VALUE>59W</D_RESULT_VALUE>
            </D_RESULT>
            <D_RESULT>
                 <D_RESULT_ID>Result.Min_Limit</D_RESULT_ID>
                 <D_RESULT_VALUE/>
            </D_RESULT>
            <D_RESULT>
                 <D_RESULT_ID>Result.Max_Limit</D_RESULT_ID>
                 <D_RESULT_VALUE>&lt;=600.0000</D_RESULT_VALUE>
            </D_RESULT>
            <D_RESULT>
                 <D_RESULT_ID>Result.FailedCriticalLvl</D_RESULT_ID>
                 <D_RESULT_VALUE>false</D_RESULT_VALUE>
            </D_RESULT>
       </RESULT>
  </RESULTS>
</TEST>

I've created a variable to store Min_Limit value if it contains the "<" character, and it works fine when the pointer is still in Min_Limit characteristic. When it flips to next characteristic (Max_Limit), the variable seems to loose its value and therefore I have no visibility of Min_Limit value anymore to assign the value to Max_Limit characteristic.

Currently, what I have is like the following:

......

    <xsl:for-each select="a:D_RESULT">
        <D_RESULT>
            <D_RESULT_ID>
                <xsl:value-of select="a:D_RESULT_ID"/>
            </D_RESULT_ID>
            <D_RESULT_VALUE>
                <xsl:value-of select="a:D_RESULT_VALUE"/>
            </D_RESULT_VALUE>
        </D_RESULT>
    </xsl:for-each>
</RESULT>

... other fields

I've found some interesting conversations here with some approaches I believe would help solving this, but I'm not sure I understood enough to apply to my scenario. This is one of those XSLT: nested for-each and dynamic variable

Would anyone suggests something to solve this?

Many thanks in advance! Rafael.

Upvotes: 1

Views: 1613

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

Here's one way you could look at it:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="D_RESULT_VALUE[../D_RESULT_ID='Result.Min_Limit' and contains(., '&lt;')] ">
    <xsl:copy/>
</xsl:template>

<xsl:template match="D_RESULT_VALUE[../D_RESULT_ID='Result.Max_Limit' and contains(../../D_RESULT[D_RESULT_ID='Result.Min_Limit']/D_RESULT_VALUE, '&lt;')] ">
    <xsl:copy>
        <xsl:value-of select="../../D_RESULT[D_RESULT_ID='Result.Min_Limit']/D_RESULT_VALUE"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Demo: http://xsltransform.net/3NzcBtA

Upvotes: 1

Related Questions