 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 15th, 2009, 05:16 PM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Date Comparison in XSLT
Hi All,
I am new to XSLT and i have following XSLT and i want to add date check..
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="exslt saxon bpws cis ihmap p1 p0" version="2.0" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/" xmlns:cis="http://www.approuter.com/schemas/2003/1/UserCallouts/" xmlns:exslt="http://exslt.org/common" xmlns:ihmap="http://www.approuter.com/xmlns/2002/Mapping" xmlns:p0="urn:/crmondemand/xml/account" xmlns:p1="urn:crmondemand/ws/account/10/2004" xmlns:saxon="http://saxon.sf.net/" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cou0="urn:/crmondemand/xml/user" xmlns:cou1="urn:crmondemand/ws/user/10/2004">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:variable name="srcDoc1" select="bpws:getVariableData('Account_Address_Loop')"/>
<xsl:variable name="CRMODUserOutPut" select="bpws:getVariableData('SSI_Account_Address_User_Output')"/>
<xsl:template match="/">
<xsl:element name="AddressCSV" namespace="">
<xsl:for-each select="$srcDoc1/p0:Account/p0:ListOfAddress/p0:Address">
<xsl:variable name="AccountModifiedId" select="$srcDoc1/p0:Account/p0:ModifiedById"/>
<xsl:if test ="string-length(p0:City)>0 and string-length(p0:Address)>0">
<xsl:element name="Accounts" namespace="">
<xsl:element name="AccountId" namespace="">
<xsl:value-of select="$srcDoc1/p0:Account/p0:AccountId"/>
</xsl:element>
<xsl:element name="AddressID" namespace="">
<xsl:value-of select="p0:AddressId"/>
</xsl:element>
<xsl:element name="Country" namespace="">
<xsl:value-of select="p0:Country"/>
</xsl:element>
<xsl:element name="Address1" namespace="">
<xsl:value-of select="p0:Address"/>
</xsl:element>
<xsl:element name="Address2" namespace="">
<xsl:value-of select="p0:StreetAddress2"/>
</xsl:element>
<xsl:element name="Address3" namespace="">
<xsl:value-of select="p0:StreetAddress3"/>
</xsl:element>
<xsl:element name="City" namespace="">
<xsl:value-of select="p0:City"/>
</xsl:element>
<xsl:element name="State" namespace="">
<xsl:value-of select="p0:StateProvince"/>
</xsl:element>
<xsl:element name="PostalCode" namespace="">
<xsl:value-of select="p0:ZipCode"/>
</xsl:element>
<xsl:element name="Province" namespace="">
<xsl:value-of select="p0:Province"/>
</xsl:element>
<xsl:element name="ExternalSystemId" namespace="">
<xsl:value-of select="p0:ExternalSystemId"/>
</xsl:element>
<xsl:element name="Salesrep" namespace="">
<xsl:value-of select="$CRMODUserOutPut/cou1:UserWS_UserQueryPage_Output/cou0:ListOfUser/cou0:User[cou0:UserId=$AccountModifiedId]/cou0:ExternalSystemId"/>
</xsl:element>
<xsl:element name="ModifiedDate" namespace="">
<xsl:value-of select="p0:ModifiedDate"/>
</xsl:element>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
i need to check if modified date is specific or greator..format is
05/13/2009 10:00:00
how can check the date in above XSLT?
Thanks in advance
|
|

May 15th, 2009, 06:07 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
XSLT 2.0 has built-in support for date handling; many XSLT 1.0 processors support the EXSLT extension library (see www.exslt.org). In both cases, however, you'll have to convert your date from local US format to international format (yyyy-mm-dd) before you can do much else with it.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

May 15th, 2009, 06:08 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I should have mentioned that you can of course do that conversion using the substring() and concat() functions.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

May 18th, 2009, 01:42 PM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Thanks Michael,
Here is what i did? Is this correct syntax wise because i am getting error on syntax..
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="exslt saxon bpws cis ihmap p1 p0" version="2.0" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/" xmlns:cis="http://www.approuter.com/schemas/2003/1/UserCallouts/" xmlns:exslt="http://exslt.org/common" xmlns:ihmap="http://www.approuter.com/xmlns/2002/Mapping" xmlns:p0="urn:/crmondemand/xml/account" xmlns:p1="urn:crmondemand/ws/account/10/2004" xmlns:saxon="http://saxon.sf.net/" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cou0="urn:/crmondemand/xml/user" xmlns:cou1="urn:crmondemand/ws/user/10/2004">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:variable name="srcDoc1" select="bpws:getVariableData('Account_Address_Loop')"/>
<xsl:variable name="CRMODUserOutPut" select="bpws:getVariableData('SSI_Account_Address_User_Output')"/>
<xsl:template match="/">
<xsl:element name="AddressCSV" namespace="">
<xsl:for-each select="$srcDoc1/p0:Account/p0:ListOfAddress/p0:Address">
<xsl:variable name="AccountModifiedId" select="$srcDoc1/p0:Account/p0:ModifiedById"/>
<xsl:variable name="AccountModifiedDate" select="concat('substring($srcDoc1/p0:Account/p0:ModifiedDate,7,4)','-','substring($srcDoc1/p0:Account/p0:ModifiedDate,4,2)','-','substring($srcDoc1/p0:Account/p0:ModifiedDate,1,2)')"/>
<xsl:if test ="string-length(p0:City)>0 and string-length(p0:Address)>0 and $AccountModifiedDate >= 2009-05-13">
<xsl:element name="Accounts" namespace="">
<xsl:element name="AccountId" namespace="">
<xsl:value-of select="$srcDoc1/p0:Account/p0:AccountId"/>
</xsl:element>
<xsl:element name="AddressID" namespace="">
<xsl:value-of select="p0:AddressId"/>
</xsl:element>
<xsl:element name="Country" namespace="">
<xsl:value-of select="p0:Country"/>
</xsl:element>
<xsl:element name="Address1" namespace="">
<xsl:value-of select="p0:Address"/>
</xsl:element>
<xsl:element name="Address2" namespace="">
<xsl:value-of select="p0:StreetAddress2"/>
</xsl:element>
<xsl:element name="Address3" namespace="">
<xsl:value-of select="p0:StreetAddress3"/>
</xsl:element>
<xsl:element name="City" namespace="">
<xsl:value-of select="p0:City"/>
</xsl:element>
<xsl:element name="State" namespace="">
<xsl:value-of select="p0:StateProvince"/>
</xsl:element>
<xsl:element name="PostalCode" namespace="">
<xsl:value-of select="p0:ZipCode"/>
</xsl:element>
<xsl:element name="Province" namespace="">
<xsl:value-of select="p0:Province"/>
</xsl:element>
<xsl:element name="ExternalSystemId" namespace="">
<xsl:value-of select="p0:ExternalSystemId"/>
</xsl:element>
<xsl:element name="Salesrep" namespace="">
<xsl:value-of select="$CRMODUserOutPut/cou1:UserWS_UserQueryPage_Output/cou0:ListOfUser/cou0:User[cou0:UserId=$AccountModifiedId]/cou0:ExternalSystemId"/>
</xsl:element>
<xsl:element name="ModifiedDate" namespace="">
<xsl:value-of select="p0:ModifiedDate"/>
</xsl:element>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Last edited by navik_pathak; May 18th, 2009 at 01:44 PM..
|
|

May 18th, 2009, 04:08 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>i am getting error on syntax..
Doesn't it even occur to you that I might find it easier to explain the error message to you if you tell me what it is?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

May 18th, 2009, 04:16 PM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Thats the thing..i am using cast iron and it says following
SEVERE: Failed to transform XML, error was: javax.xml.transform.TransformerConfigurationExcept ion: Failed to compile stylesheet. 2 errors detected.
com.approuter.common.transformation.Transformation EngineException: javax.xml.transform.TransformerConfigurationExcept ion: Failed to compile stylesheet. 2 errors detected.
if i open this XSLT in Altova XML spy it doesnt give any error but at run time when i am applying this it gives me error....
|
|

May 18th, 2009, 04:32 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
OK, thanks, that's useful to me even if it wasn't useful to you. The message "Failed to compile stylesheet. 2 errors detected." is the text of an exception thrown by Saxon at the end of an unsuccessful compilation; the detailed errors will have been reported to Cast Iron. I don't know what it did with them - you'll have to look in the documentation for the application - they have probably been written to a log file somewhere.
If you can't find the documentation or the log file, you can try compiling the stylesheet in Saxon directly, outside the application environment. This gives:
Error at xsl:variable on line 4 column 89 of test.xsl:
XPST0017: XPath syntax error at char 44 on line 4 in {...leData('Account_Address_Loo...}:
Cannot find a matching 1-argument function named
{http://schemas.xmlsoap.org/ws/2003/03/business-process/}getVariableData()
Error at xsl:variable on line 5 column 108 of test.xsl:
XPST0017: XPath syntax error at char 55 on line 5 in {..._Account_Address_User_Outpu...}:
Cannot find a matching 1-argument function named
{http://schemas.xmlsoap.org/ws/2003/03/business-process/}getVariableData()
Error at xsl:if on line 11 column 117 of test.xsl:
XPTY0004: Cannot compare xs:string to xs:integer
Error at xsl:template on line 6 column 29 of test.xsl:
XPTY0004: Cannot compare xs:string to xs:integer
Failed to compile stylesheet. 4 errors detected.
The failure to find the extension function getVariableData() may be spurious (the function may be present when the stylesheet is compiled in your real environment). The other error looks real, though it is probably one error being reported twice. The offending code is
$AccountModifiedDate >= 2009-05-13
Here 2009-05-13 is being taken as an arithmetic expression that returns the integer 1991, whereas $AccountModifiedDate is known to be a string because it is the result of the concat() function. So Saxon's type checking is coming to your rescue here. What you want is to compare two dates, so convert both arguments to xs:date:
xs:date($AccountModifiedDate) >= xs:date('2009-05-13')
Rather than just fixing your stylesheet code, I strongly suggest you fix your development environment so that if you make syntax errors again, as you inevitably will, you will get proper error messages explaining them. It may be worth asking Cast Iron for advice on this.
Michael Kay
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

May 19th, 2009, 02:20 PM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Thanks Michael for your help i have solved the comparison following way...
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="exslt saxon bpws cis ihmap p1 p0" version="2.0" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/" xmlns:cis="http://www.approuter.com/schemas/2003/1/UserCallouts/" xmlns:exslt="http://exslt.org/common" xmlns:ihmap="http://www.approuter.com/xmlns/2002/Mapping" xmlns:p0="urn:/crmondemand/xml/account" xmlns:p1="urn:crmondemand/ws/account/10/2004" xmlns:saxon="http://saxon.sf.net/" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cou0="urn:/crmondemand/xml/user" xmlns:cou1="urn:crmondemand/ws/user/10/2004">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:variable name="srcDoc1" select="bpws:getVariableData('Account_Address_Loop')"/>
<xsl:variable name="CRMODUserOutPut" select="bpws:getVariableData('SSI_Account_Address_User_Output')"/>
<xsl:template match="/">
<xsl:element name="AddressCSV" namespace="">
<xsl:for-each select="$srcDoc1/p0:Account/p0:ListOfAddress/p0:Address">
<xsl:variable name="AccountModifiedId" select="$srcDoc1/p0:Account/p0:ModifiedById"/>
<xsl:variable name="AccountModifiedDate" select="concat(substring(string(p0:ModifiedDate),7,4),'-',substring(string(p0:ModifiedDate),1,2),'-',substring(string(p0:ModifiedDate),4,2))"/>
<xsl:if test ="string-length(p0:City)>0 and string-length(p0:Address)>0 and (number(translate($AccountModifiedDate,'-',''))-number(translate('2009-05-19','-','')))>=0">
<xsl:element name="Accounts" namespace="">
<xsl:element name="AccountId" namespace="">
<xsl:value-of select="$srcDoc1/p0:Account/p0:AccountId"/>
</xsl:element>
<xsl:element name="AddressID" namespace="">
<xsl:value-of select="p0:AddressId"/>
</xsl:element>
<xsl:element name="Country" namespace="">
<xsl:value-of select="p0:Country"/>
</xsl:element>
<xsl:element name="Address1" namespace="">
<xsl:value-of select="p0:Address"/>
</xsl:element>
<xsl:element name="Address2" namespace="">
<xsl:value-of select="p0:StreetAddress2"/>
</xsl:element>
<xsl:element name="Address3" namespace="">
<xsl:value-of select="p0:StreetAddress3"/>
</xsl:element>
<xsl:element name="City" namespace="">
<xsl:value-of select="p0:City"/>
</xsl:element>
<xsl:element name="State" namespace="">
<xsl:value-of select="p0:StateProvince"/>
</xsl:element>
<xsl:element name="PostalCode" namespace="">
<xsl:value-of select="p0:ZipCode"/>
</xsl:element>
<xsl:element name="Province" namespace="">
<xsl:value-of select="p0:Province"/>
</xsl:element>
<xsl:element name="ExternalSystemId" namespace="">
<xsl:value-of select="p0:ExternalSystemId"/>
</xsl:element>
<xsl:element name="Salesrep" namespace="">
<xsl:value-of select="$CRMODUserOutPut/cou1:UserWS_UserQueryPage_Output/cou0:ListOfUser/cou0:User[cou0:UserId=$AccountModifiedId]/cou0:ExternalSystemId"/>
</xsl:element>
<xsl:element name="ModifiedDate" namespace="">
<xsl:value-of select="number(translate($AccountModifiedDate,'-',''))"/>
</xsl:element>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
i have a question on parametarized XSLT as the date i am comparing it to is dynamic. I have little idean that i have to generate date in an XML and use it into XSLT. Can you please let me know how can generate an XML with respect to xmlns and its xsd format...i have following XML format
Code:
<?xml version="1.0" encoding="UTF-8"?>
<RunTime>
<Header/>
<RunTime>2009-05-19</RunTime>
</RunTime>
how can i use above XML in XSLT?
THanks in advance
|
|
 |