Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old October 3rd, 2007, 02:43 PM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default What's wrong here?

In my system there is a servlet that requests info from a db and then makes an xml file with user info. In that info is a phone number. If the user's phone number is marked as private then (xxx) xxx-xxxx is returned. I'm trying to make my xsl so that instead of displaying x's it displays nothing. This is what I'm trying:


<xsl:variable name="phone">
 <xsl:value-of select="response/bio_contents/phone_number" disable-output-escaping="yes" />
</xsl:variable>
<xsl:variable name="isempty">
 <xsl:value-of select="compare('(xxx) xxx-xxxx',$phone)"/>
</xsl:variable>

<xsl:if test="$isempty != 1">
 <xsl:value-of select="$phone"/>
</xsl:if>

It seems to be valid but it's causing an error because nothing on my page shows up. If I delete this, all the rest of the user's information shows just fine.
 
Old October 3rd, 2007, 02:55 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

A few points.

(a) don't use disable-output-escaping. It's completely undefined when writing to a temporary destination, as here, and it certainly does no good. And it makes you like like a programmer who will try anything rather than resort to reading the manual.

(b) don't use the construct

<xsl:variable name="x">
  <xsl:value-of select="y"/>
</xsl:variable>

when you mean <xsl:variable name="x" select="y"/>.

Unless of course you get paid more for writing three lines of code where one will do, or for writing a program that takes three times longer to execute.

(c) don't use the compare() function to test for equality. compare() is an XSLT 2.0 function that returns -1 if a<b, 0 if a=b, and +1 if a>b. I think you're testing for equality, so that should give 0, not 1 as the answer, but you can test for equality using "=".

(d) don't post here saying that something causes an error without saying what the error is. You might not understand it, but the chances are that someone else does.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 3rd, 2007, 02:59 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You should be checking $isempty != 0, not 1 as compare returns -1, 0 or 1.

Other than that I'm not sure as you don't say what the error is.

I think the following probably does the same on one line though.

<xsl:value-of select="response/bio_contents/phone_number[text() != '(xxx) xxx-xxxx']"/>

/- Sam Judson : Wrox Technical Editor -/
 
Old October 3rd, 2007, 03:12 PM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I didn't have an error to give you. A servlet takes the page and the xsl and does the transformation, if there is an error, it just outputs "user not available" and I don't have immediate access to the logs.

Anyway, I got it working using contains instead of compare. Must have been because of the 2.0 issue.

I didn't make the XSL, it's just a standard one we use, but one subweb didn't like having x's for no phone number, they wanted nothing. So I was just looking for a quick solution, which is working now.

Is that construct bad because it makes for messy code, or is there a performance difference?
 
Old October 3rd, 2007, 03:42 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by nero
Is that construct bad because it makes for messy code, or is there a performance difference?
If you mean the variable declaration then both.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
What's wrong ??? FT BOOK: ASP.NET Website Programming Problem-Design-Solution 2 November 3rd, 2005 09:18 AM
Help..What am I doing wrong... Brettvan1 VB.NET 2002/2003 Basics 2 October 18th, 2004 02:36 AM
Where did I go wrong??? ahc2inc VB.NET 2002/2003 Basics 3 September 28th, 2004 08:19 PM
What's wrong?Help! amu BOOK: Beginning ASP.NET 1.0 1 October 28th, 2003 08:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.