 |
| 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
|
|
|
|

April 8th, 2010, 02:22 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Using if condition in XSLT
Hi folks,
I am new to XSLT.
Could any one please respond to my query
1. I have an XML as follows
<LMP Request>
<LMP Message>
<error text>product is not present></errortext>
<errorCount>1<errorcount>
</LMP Message>
<LMP Request>
2. outputXML should be like if errorcount is equal to 1,the output XML should have tag called ErrorDetails where in the error text value is the value like
<ErrorDetails> value of the error text</ErrorDetails> and one more tag called <Success>N</Success>. If error count is equal to 0 then <success> tag should have a value Y.
I tried like <xsl:if match=".[errorCount=1]
<xsl:value of "LMPRequest/LMP Message/errortext"
</xsl :if>
even though the input xml has a tag error count =1 , i am not able to print error text.
Could any one please help me in writing the XSLT.
Thanks in advance
|
|

April 8th, 2010, 02:38 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>I tried like <xsl:if match=".[errorCount=1]"
Where are you getting your information from? This is the syntax from a 1998 draft of the XSLT specification, which was current for less than a year!
The correct syntax is <xsl:if test="errorCount=1">
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

April 9th, 2010, 05:09 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Michael,
Thanks for the response.
I tried the syntax you gave but i am not getting correct xml.
i am attaching input xml, xslt and output xml.
Please correct me if i am wrong.
Input xml:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LMPResponse xmlns="http://lmps.com/lmp/webservices">
<LMPResult>
<errorInfo errText="Error : A product code must be supplied when adding a portfolio"/>
<warningCount>0</warningCount>
<errorCount >1</errorCount>
</LMPResult>
</LMPResponse>
</soap:Body>
</soap:Envelope>
Required outut xml:
<LMPReply>
<LMPResponses>
<LMPResponse>
<LMPReturn>
<LMPMessage>createPortfolio <Section RowCount="0">Header</Section>
<Section RowCount="1">Details<Row>
<SUCCESS><![CDATA[N]]></SUCCESS>
<ErrorDetail MESSAGE="the error text you've copied from the LMPRequest" />
</Row>
</Section>
<Section RowCount="0">Footer</Section>
</LMPMessage>
</LMPReturn>
</GTSResponse>
</LMPResponses>
</LMPReply>
XSLT i wrote:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:o="services">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no" />
<xsl:template match="/soap:Envelope">
<xsl:apply-templates select="soap:Body"/>
</xsl:template>
<xsl:template match="soap:Body">
<LMPReply>
<LMPResponses>
<LMPResponse>
<LMPReturn>
<LMPMessage>AddPortfolio <Section RowCount="0">Header</Section>
<Section RowCount="1">Details<Row>
<xsl:if test="errorCount=1">
<ErrorDetails><xsl:value-of select="o:CreatePortfolioResponse /o:CreatePortfolioResult/o:errorInfo"/></ErrorDetails>
<SUCCESS>N</SUCCESS>
</xsl:if>
</Row>
</Section>
<Section RowCount="0">Footer</Section>
<LMPMessage>
</LMPReturn>
</LMPResponse>
</LMPResponses>
</LMPReply>
</xsl:template>
</xsl:stylesheet>
The output i got is
<?xml version="1.0" encoding="UTF-8"?>
<LMPReply xmlns:o="services" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<LMPResponses>
<LMPResponse>
<LMPReturn>
<LMPMessage>AddPortfolio <Section RowCount="0">Header</Section>
<Section RowCount="1">Details<Row />
</Section>
<Section RowCount="0">Footer</Section>
</LMPMessage>
</LMPReturn>
</LMPResponse>
</LMPResponses>
</LMPReply>
Could you please help in writing the correct XSLT.
Basing on the value of ErrorCount i. e if ErrorCount = 0 then in the output xml i should get like only <Success>Y<Success> else i.e if ErrorCount !=0 then the output xml should the error details tag with error text and <Success>N</Success> tag.
Please help me.
Thanks,
Divya
|
|

April 9th, 2010, 05:24 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well firstly there are things in your XSLT that are referred to that are not in your input XML, specifically the xmlns:o="services" namespace, and the o:CreatePortfolioResponse element.
Your input document specifies that all the elements inside the soap body are in the http://lmps.com/lmp/webservices namespace, but you don't define that in the XSLT.
Get all these things right, and add the namespace to the errorCount reference and you will probably have better luck.
|
|

April 9th, 2010, 05:42 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
XPath expressions used in the test attribute of xsl:if follow the same context rules as XPath expressions anywhere else. Your context node is an soap:Body element, so you need to select the errorInfo element relative to that element: something like w:LMPResponse/w:LMPResult/w:errorInfo where prefix w is bound to namespace http://lmps.com/lmp/webservices.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

April 9th, 2010, 06:19 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Micheal,
I added like <ErrorDetails><xsl:value-of select="o:LMPResponse /o:LMPResult/o:errorInfo"/></ErrorDetails> even now i am not getting the expected output.
Thanks,
Divya
|
|

April 9th, 2010, 06:21 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Sam,
I corrected the name space and repalced createprotfolioresponse with LMPREquest. even now i am not getting the output.
Thanks in advance,
divya
|
|

April 9th, 2010, 06:23 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
a) did you change the namespace associated with the 'o' prefix? Because if you didn't then nothing would ever get matched as that namespace doesn't exist in your input XML.
b) Did you change the <xsl:if> line to also use the correct namespace and element?
Code:
<xsl:if test="o:LMPResponse/o:LMPResult/o:errorCount = 1">
|
|

April 9th, 2010, 07:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
You can change yous xslt code like the below and check:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:o="http://lmps.com/lmp/webservices" extension-element-prefixes="soap o">
<xsl:output method="html" indent="yes"/>
<xsl:template match="soap:Envelope">
<xsl:apply-templates select="soap:Body"/>
</xsl:template>
<xsl:template match="soap:Body">
<LMPReply>
<LMPResponses>
<xsl:apply-templates select="o:LMPResponse"/>
</LMPResponses>
</LMPReply>
</xsl:template>
<xsl:template match="o:LMPResponse">
<LMPResponse>
<LMPReturn>
<LMPMessage>AddPortfolio <Section RowCount="0">Header</Section>
<Section RowCount="1">Details
<Row>
<xsl:if test="o:LMPResult/o:errorCount = '1'">
<ErrorDetails><xsl:attribute name="MESSAGE"><xsl:value-of select="o:LMPResult/o:errorInfo/@errText"/></xsl:attribute></ErrorDetails>
<SUCCESS>N</SUCCESS>
</xsl:if>
<xsl:if test="o:LMPResult/o:errorCount = '0'">
<ErrorDetails><xsl:attribute name="MESSAGE"><xsl:value-of select="o:LMPResult/o:errorInfo/@errText"/></xsl:attribute></ErrorDetails>
<SUCCESS>Y</SUCCESS>
</xsl:if>
</Row>
</Section>
<Section RowCount="0">Footer</Section>
</LMPMessage>
</LMPReturn>
</LMPResponse>
</xsl:template>
If possible change the xslt version to 2.0. It will be much helpful when you move further with XSLT.
__________________
Rummy
|
|

April 9th, 2010, 08:08 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hi Rummy,
Thanks a lot.
<?xml version="1.0" encoding="UTF-8"?>
<LMPResponse xmlns:o="services" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<LMPReturn>
<LMPMessage>AddPortfolio <Section RowCount="0">Header</Section>
<Section RowCount="1">Details
<Row />
</Section>
<Section RowCount="0">Footer</Section>
</LMPMessage>
</LMPReturn>
</LMPResponse>
I am not getting the error details tag.
i am using Altova XML Spy .
Thanks,
Divya
|
|
 |