XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Neal WaltersSubject: Simplifying this long if statement (Part 2)
Author: Neal Walters
Date: 19 May 2006 10:55 AM

Recently I asked for some help regarding a long if statement, and Ivan sent me a "Deep Diff Compare" callable template (which I will use), but I still have some long nasty if statements that are comparing just a few of the variables, not all of them.

It looks something like this:

<xsl:if test="(//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='Address1'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='Address1']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='Address2'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='Address2']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='Address3'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='Address3']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='Address4'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='Address4']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='City'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='City']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='CountryDivision'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='CountryDivision']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='Country'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='Country']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='PostalCode'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='PostalCode']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='HomePhone'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='HomePhone']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='HomePhone']/@Preferred != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='HomePhone']/@Preferred) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='WorkPhone'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='WorkPhone']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='WorkPhone']/@Preferred != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='WorkPhone']/@Preferred) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='WorkPhone']/@Extension != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='WorkPhone']/@Extension) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='CellPhone'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='CellPhone']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='CellPhone']/@Preferred != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='CellPhone']/@Preferred) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='PreferredEmail'] != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='PreferredEmail']) or (//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='PreferredEmail']/@EmailType != //*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='PreferredEmail']/@EmailType) or not(//*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='PreferredEmail']) or not(//*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='HomePhone']) or not(//*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='WorkPhone']) or not(//*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='CellPhone']) ">

In English it is basically saysing - Is the old address = to the new address, and is the old homephone = to the new homephone and...etc...
There are two profiles, an OriginalProfile and an Profile (with the updates in it). Both contain the same elements.

When you press the format button in Stylus Studio, all this code goes to oneline. So it would really be cool if the formatter could honor line breaks, and maybe just handle the indention.

I'm debating if I should try to simplify this line. The problem is that you have to scroll almost 3900 characters back and forth to be able to read it, which to me is next to impossible.

Should I just create a bunch of vars like this, and then use the vars in the if statement?

<xsl:variable name="vUpdAddress1">
<xsl:value-of select="//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']/*[local-name()='Address1']"/>
</xsl:variable>
<xsl:variable name="vOrigAddress1">
<xsl:value-of select="//*[local-name()='OriginalProfile']/*[local-name()='Profile']/*[local-name()='Address1']"/>
</xsl:variable>

Or I suppose I could right another callable-template where you pass it a list of variables to compare?

Any other ideas how to cut this if statement down to size?
I've actually considering just calling c# to do this transform.

Thanks,
Neal Walters
http://Biztalk-Training.com - Learn Biztalk Faster
http://Sharepoint-Training.com - End User Intro to Sharepoint (Videos)




Postnext
Ivan PedruzziSubject: Simplifying this long if statement (Part 2)
Author: Ivan Pedruzzi
Date: 19 May 2006 09:25 PM
Originally Posted: 19 May 2006 04:35 PM
Neal,

This expression is unmanageable, for your own sanity you should change approach :)

1) use full expression rather then absolute

instead of

//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']

use

ProfileUpdateRequest/Profile


2) Create a template to compare two "Profile" elements

<xsl:template name="Compare">
<xsl:param name="Original"/>
<xsl:param name="UpdateRequest"/>
<xsl:choose>
<xsl:when test="$Original/Address1 != $UpdateRequest/Address1">false</xsl:when>
<xsl:when test="$Original/Address2 != $UpdateRequest/Address2">false</xsl:when>
... more tests
</xsl:choose>
</xsl:template>

then invoke the template like this

<xsl:variable name="IsDifferent">
<xsl:call-template name="Compare">
<xsl:with-param name="Original" select="OriginalProfile/Profile"/>
<xsl:with-param name="UpdateRequest" select="ProfileUpdateRequest/Profile"/>
</xsl:call-template>
</xsl:variable>

<xsl:if test="$IsDifferent = 'false'">
<xsl:text>profiles are different</xsl:text>
</xsl:if>


Ivan Pedruzzi
Stylus Studio Team

Postnext
Neal WaltersSubject: Simplifying this long if statement (Part 2)
Author: Neal Walters
Date: 01 Jun 2006 05:45 PM
You suggested:
1) use full expression rather then absolute

instead of

//*[local-name()='ProfileUpdateRequest']/*[local-name()='Profile']

use

ProfileUpdateRequest/Profile

Given that I have a xmlns="xxxx" without a prefix, is this possible.
See attached file.

Thanks again,
Neal



UnknownProfileCellPhoneDeleted.xml
Sample XML with no-prefix namespace

Postnext
Ivan PedruzziSubject: Simplifying this long if statement (Part 2)
Author: Ivan Pedruzzi
Date: 01 Jun 2006 06:05 PM
Hi Neal,

You just need to bind the namespace for instance in the stylesheet element

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ur="http://schemas.compassion.com/constituent/profile/updaterequest/2005-03-01"
xmlns:pr="http://schemas.compassion.com/constituent/profile/profile/2005-03-01"
exclude-result-prefixes="ur pr"
>

then you will be able to use an expression like the following

ur:ProfileUpdateRequest/pr:Profile

Remeber you can use any prefix you like as long as it is bound to the right namespace.

Hope this helps
Ivan Pedruzzi
Stylus Studio Team

Postnext
Neal WaltersSubject: Simplifying this long if statement (Part 2)
Author: Neal Walters
Date: 02 Jun 2006 04:38 PM
This is where StylusStudio needs to be more integrated. I used the right-click "copy xpath to clipboard" to generate the Xpath. I think I'm the one that reported that originally the Xpath was wrong because it didn't contain the local-name() clauses, and you made an enhancement. One needs to be able to test the xpath outside of XSLT and then paste into XSLT.

Neal

Posttop
Ivan PedruzziSubject: Simplifying this long if statement (Part 2)
Author: Ivan Pedruzzi
Date: 02 Jun 2006 05:19 PM

You have been heard.

The upcoming Stylus Studio 2006 Release 3 has a far improved XPath evaluator.

Here a sneak preview



Ivan Pedruzzi
Stylus Studio Team

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.