Re: [xsl] Comparing before and after records [SEC=UNCLASSIFIED]

Subject: Re: [xsl] Comparing before and after records [SEC=UNCLASSIFIED]
From: Enrico.Raymund@xxxxxxxxxxx
Date: Mon, 7 Jul 2008 17:33:33 +1000
Hi Abel,

I was not able to come back to you sooner but here it is:

XML Input:
<?xml version="1.0" encoding="UTF-8"?>
<Events>
    <Before>
        <Event_Type>A</Event_Type>
        <Event_Field>1111</Event_Field>
        <Event_Id>1001</Event_Id>
    </Before>
    <Before>
        <Event_Type>B</Event_Type>
        <Event_Field>2222</Event_Field>
        <Event_Id>1002</Event_Id>
    </Before>
    <After>
        <Event_Type>A</Event_Type>
        <Event_Field>1111</Event_Field>
        <Event_Id>1001</Event_Id>
    </After>
    <After>
        <Event_Type>B</Event_Type>
        <Event_Field>2233</Event_Field>
        <Event_Id>1002</Event_Id>
    </After>
    <After>
        <Event_Type>C</Event_Type>
        <Event_Field>1122</Event_Field>
        <Event_Id>1003</Event_Id>
    </After>
</Events>

The XSL file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/>
    <xsl:strip-space elements="*"/>
    <xsl:key match="/Events/Before" name="BEFORE_ID" use="Event_Id"/>
    <!-- Root Template -->
    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="count(//After) &gt; 0">
                <xsl:for-each select="//After">
                    <xsl:choose>
                        <xsl:when test="key('BEFORE_ID', Event_Id)">
                            <xsl:call-template
name="INSTANTIATE_MODIFY_EVENT">
                                <xsl:with-param name="EVENT_TYPE"
select="Event_Type"/>
                                <xsl:with-param name="AFTER_EVENT_ID"
select="Event_Id"/>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template
name="INSTANTIATE_CREATE_EVENT">
                                <xsl:with-param name="EVENT_TYPE"
select="Event_Type"/>
                                <xsl:with-param name="AFTER_EVENT_ID"
select="Event_Id"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                <!-- No occurence of 'After':  there is nothing to output
-->
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!--=============================================================-->
    <!-- NAMED TEMPLATES                                             -->
    <!--=============================================================-->
    <xsl:template name="INSTANTIATE_MODIFY_EVENT">
        <!-- MODIFY -->
        <xsl:param name="EVENT_TYPE"/>
        <xsl:param name="AFTER_EVENT_ID"/>
        <xsl:result-document encoding="utf-8"
href="{$EVENT_TYPE}_{$AFTER_EVENT_ID}.xml"
            indent="yes" method="xml">
            <Event>
                <Operation>MODIFY</Operation>
                <xsl:choose>

                    <!-- ### THIS IS WHERE I NEED HELP ###-->
                    <!-- NEED TO TAKE INTO CONSIDERATION the Event_Id of
the current (After) context -->
                    <!-- CAN THIS BE DONE WITHOUT USING key() and
generate-id()? -->
                    <xsl:when test="//Before/Even_Field/text() =
Event_Field/text()">
                        <xsl:comment>unchanged</xsl:comment>
                    </xsl:when>
                    <!-- ### THIS IS WHERE I NEED HELP ###-->

                    <xsl:otherwise>
                        <xsl:comment>changed</xsl:comment>
                    </xsl:otherwise>
                </xsl:choose>
                <Field>
                    <xsl:value-of select="Event_Field"/>
                </Field>
            </Event>
        </xsl:result-document>
        <!-- CREATE -->
    </xsl:template>

    <xsl:template name="INSTANTIATE_CREATE_EVENT">
        <xsl:param name="EVENT_TYPE"/>
        <xsl:param name="AFTER_EVENT_ID"/>
        <xsl:result-document encoding="utf-8"
href="{$EVENT_TYPE}_{$AFTER_EVENT_ID}.xml"
            indent="yes" method="xml">
            <Event>
                <Operation>CREATE</Operation>
                <Field>
                    <xsl:value-of select="Event_Field"/>
                </Field>
            </Event>
        </xsl:result-document>
    </xsl:template>
</xsl:stylesheet>

 The requirements are as follows:

(1) Create a separate XML file for each "After" instance.
(2) If an "After" instance has no corresponding "Before" instance, related
by EventId, then this a create operation, ie, <Operation>CREATE</Operation>
(3) If an  "After" instance has a corresponding "Before" instance, related
by EventId, then this a modify operation, ie, <Operation>MODIFY</Operation>
      (3.1) If the value of the Before/Event_Field != to After/Event_Field,
put a comment:  "<!--changed-->"
      (3.2) If the value of the Before/Event_Field = to After/Event_Field,
put a comment:  "<!--unchanged-->"

The code between "<!-- ### THIS IS WHERE I NEED HELP ###-->" is where I
need a proper XPath put in.  With the input XML, the XSLT creates 3 XML
files -- which it does,  the XSLT created the following files:

A_1001.xml  [the comment is wrong (as the XPath is wrong):
Before/EventField = 1111; After/EventField = 1111 (unchanged)]

<?xml version="1.0" encoding="utf-8"?>
<Event>
    <Operation>MODIFY</Operation>
    <!--changed-->
    <Field>1111</Field>
</Event>


B_1002.xml [the comment field is correct:   Before/EventField = 2222;
After/EventField = 2233 (changed)]

<?xml version="1.0" encoding="utf-8"?>
<Event>
    <Operation>MODIFY</Operation>
    <!--changed-->
    <Field>2233</Field>
</Event>

C_1003.xml [This is correct because there is no <Before> for
EventId="1003"]

<?xml version="1.0" encoding="utf-8"?>
<Event>
    <Operation>CREATE</Operation>
    <Field>1122</Field>
</Event>

Thanks in advance.

Enrico




                                                                           
             Abel Braaksma                                                 
             <abel.online@xs4a                                             
             ll.nl>                                                     To 
                                            xsl-list@xxxxxxxxxxxxxxxxxxxxx 
             03/07/2008 05:22               m                              
             PM                                                         cc 
                                                                           
                                                                   Subject 
             Please respond to              Re: [xsl] Comparing  before    
             xsl-list@xxxxxxxx              and after records              
              lberrytech.com                [SEC=UNCLASSIFIED]             
                                                           Protective Mark 
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           



Enrico.Raymund@xxxxxxxxxxx wrote:
>
> The creation of the multiple output document and the transformations are
> easy.  The help I need is in comparing the <Before> and <After> contents
> with the same <EventId>.

Your example input and output looks excellent and clearly indented, but
please also show us your current xslt file, so we can help you with
where you went wrong (and explain what you tried so we can understand
where you're coming from). Now it's just a guessing game... (see the
list guidelines for more help).





--------------------------------------------------------------------
Important Notice: If you have received this email by mistake, please advise the sender and delete the message and attachments immediately.  This email, including attachments, may contain confidential, sensitive, legally privileged and/or copyright information.  Any review, retransmission, dissemination or other use of this information by persons or entities other than the intended recipient is prohibited.  DIAC respects your privacy and has obligations under the Privacy Act 1988.  The official departmental privacy policy can be viewed on the department's website at www.immi.gov.au. See: http://www.immi.gov.au/functional/privacy.htm

---------------------------------------------------------------------

Current Thread