Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 26th, 2006, 02:15 AM
Registered User
 
Join Date: Mar 2006
Location: Sunland, California, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default counting in XSL

Hi,
I looked far and wide on how to solve this problem, so now I have to ask ;)

I have all my address data in an xml file and have succeded with a nice xslt to transform and display the addressdata in any browser in well human-readable form.

Now I wanted to use the same xml data to create a file with vCard entries. Not so difficult, except that there are some of my fieds that do not map to any vCard field and also I have several NOTE fields while vCard has only one. So I have to collect all the NOTES and additional fields and stick them all into the one NOTE field in vCard.

For this sticking I have to concatenate all these elements with a =0A=0D as line separator, but the problem is that the first element that goes into that NOTE field has to be different. So I need something to flag the first usage of my template that matches all these extra fields.

I need something like (where all these elements are the ones I want to group into the one NOTE element):

<xsl:template match="NO|MS|OF|VM|WB|PG|BX|BB|CT">
if first time then
NOTE;ENCODING=QUOTED-PRINTABLE:<xsl:value-of select="."/>
else
=0D=0A<xsl:value-of select="."/>
</xsl:template>

Ho do you do this in in XSLT - I tried xsl:script but this appears to work only under conditions that are not true for me, like IE/MSXML - I use firefox.

Thanks for any hint you might be able to give me.
Gunter


Reply With Quote
  #2 (permalink)  
Old March 26th, 2006, 10:03 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If all the calls on this template result from a single call on xsl:apply-templates, then you can use the test

<xsl:choose>
 <xsl:when test="position()=1">
  ...
 <xsl:otherwise>...
</xsl:choose>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old March 27th, 2006, 12:11 AM
Registered User
 
Join Date: Mar 2006
Location: Sunland, California, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Michael,

yes, I had tried the position() function and this did not do the job. The results of postion were low numbers that I interpreted to be the position of the interpreted xml file, not the position of the output.

I had also looked (that was actually my first idea) in defining a variable but then I have not found anything to reassign a value to a variable, they seem to be more like constants.

so, no luck so far in getting this under control.

Gunter


Reply With Quote
  #4 (permalink)  
Old March 27th, 2006, 05:37 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

DIfficult to give more advice without seeing the detail. The other techniques to look at are <xsl:number>, and simply computing expressions like

count(preceding-sibling::*[.... some predicate ...])

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old March 28th, 2006, 06:59 PM
Registered User
 
Join Date: Mar 2006
Location: Sunland, California, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

I really appreciate that you want to help me out there, because this
is really something that I should learn and know.

I made a little excerpt of my address file with only two entries,
the first one maps great to a vcard, but the second makes quite some
problems.

Here I have the elements BB and NO that I would like to get all into
the NOTE element of the vCard.

In my included xsl file below I now only look for the last NO and the
other unknown elements are just bypassed.

I have long years of programming on my back, everything assembler to
C++, but with all this I can't seem to find a way to handle this little
problem, and I am so thick-headed now that I want to solve it with
xsl instead of just writing a C program <grin>

Thanks
Gunter


==== file allad.xml ===============================================

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="allad.xsl"?>

<ALLAD>
  <MARK>
    <LETTER>====A====</LETTER>

    <ADD>
      <CO>Able Avionics</CO>
      <BS>16644 Roscoe Blvd.</BS>
      <BC>Van Nuys</BC>
      <BT>California</BT>
      <BZ>91406</BZ>
      <BP>(818) 779-0265</BP>
    </ADD>

  </MARK>

  <MARK>
    <LETTER>====C====</LETTER>

    <ADD>
      <CO>CompuServe</CO>
      <BP>(800) 848-8990</BP>
      <BB>(800) 346-3147</BB>
      <BB>(909) 865-0553</BB>
      <BB>(818) 988-9791</BB>
      <NO>112465,3713 User Number</NO>
      <NO>port#: 4144</NO>
    </ADD>

  </MARK>

</ALLAD>


==== file allad.xsl ================================================

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/">
<HTML><BODY><pre>
<xsl:apply-templates/>
</pre></BODY></HTML>
</xsl:template>

<xsl:template match="MARK"><xsl:apply-templates select="ADD"/></xsl:template>

<xsl:template match="ADD">
BEGIN:VCARD
VERSION:2.1
<xsl:choose>
<xsl:when test="FN or LN">N:<xsl:value-of select="LN"/>;<xsl:value-of select="FN"/>;<xsl:value-of select="MN"/>;<xsl:value-of select="TI"/>;
FN:<xsl:value-of select="TI"/> <xsl:if test="TI">#160;</xsl:if> <xsl:value-of select="FN"/> <xsl:if test="FN">#160;</xsl:if> <xsl:value-of select="MN"/> <xsl:if test="MN">#160;</xsl:if> <xsl:value-of select="LN"/> #160;
<xsl:if test="CO">ORG:<xsl:value-of select="CO"/>#160;<xsl:value-of select="DS"/></xsl:if>
</xsl:when>
<xsl:otherwise>N:<xsl:value-of select="CO"/>;<xsl:value-of select="DS"/>;;;
FN:<xsl:value-of select="CO"/> <xsl:value-of select="DS"/></xsl:otherwise>
</xsl:choose>

<xsl:apply-templates select="HS|BS|PH|VM|FX|MB|WB|PG|EM|BP|BF|BX|WW|BB| CT|NO"/>
END:VCARD
</xsl:template>


<xsl:template match="HS">
ADR;HOME:<xsl:value-of select="../HM"/>;;<xsl:value-of select="."/>;<xsl:value-of select="../HC"/>;<xsl:value-of select="../HT"/>;<xsl:value-of select="../HZ"/>;<xsl:value-of select="../H0"/>
LABEL;HOME;ENCODING=QUOTED-PRINTABLE:<xsl:value-of select="."/>=0D=0A<xsl:value-of select="../HC"/>, <xsl:value-of select="../HT"/> <xsl:value-of select="../HZ"/>=0D=0A<xsl:value-of select="../HO"/>
</xsl:template>

<xsl:template match="BS">
ADR;WORK:<xsl:value-of select="../BM"/>;<xsl:value-of select="../MS"/>;<xsl:value-of select="."/>;<xsl:value-of select="../BC"/>;<xsl:value-of select="../BT"/>;<xsl:value-of select="../BZ"/>;<xsl:value-of select="../B0"/>
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:<xsl:value-of select="../MS"/>=0D=0A<xsl:value-of select="."/>=0D=0A<xsl:value-of select="../BC"/>, <xsl:value-of select="../BT"/> <xsl:value-of select="../BZ"/>=0D=0A<xsl:value-of select="../BO"/>
</xsl:template>

<xsl:template match="JT">
TITLE:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="BP">
TEL;WORK;VOICE:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="BP2">
TEL;WORK;VOICE:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="PH">
TEL;HOME;VOICE:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="MB">
TEL;CELL;VOICE:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="BF">
TEL;WORK;FAX:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="FX">
TEL;HOME;FAX:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="WW">
URL;WORK:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="BD">
BDAY:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="EM">
EMAIL;PREF;INTERNET:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="NO">
NOTE;ENCODING=QUOTED-PRINTABLE:<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="MS|OF|VM|WB|PG|BX|BB|CT">
</xsl:template>

</xsl:stylesheet>

================================================== ====================

Reply With Quote
  #6 (permalink)  
Old March 29th, 2006, 03:55 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As I'm not familiar with the vCard format it might make things clearer if you show what output you want to achieve.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #7 (permalink)  
Old March 29th, 2006, 04:34 AM
Registered User
 
Join Date: Mar 2006
Location: Sunland, California, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OOPS - sure, should have thought of that.
This is the full set of parameters coming out of outlook which I used during my reverse engineering ;)

Gunter

BEGIN:VCARD
VERSION:2.1
N:TestEntry;Firstname;Middle;Dr.;III
FN:Firstname Middle TestEntry III
NICKNAME:Nich
ORG:Company;department
TITLE:Job Title
NOTE;ENCODING=3DQUOTED-PRINTABLE:=3D0D=3D0A
TEL;WORK;VOICE:+1 (818) 555-1212
TEL;WORK;VOICE:+1 (818) 555-1216
TEL;HOME;VOICE:+1 (818) 555-1213
TEL;CELL;VOICE:+1 (818) 555-1215
TEL;CAR;VOICE:+1 (818) 555-1219
TEL;VOICE:+1 (818) 555-1224
TEL;PAGER;VOICE:+1 (818) 555-1226
TEL;WORK;FAX:+1 (818) 555-1214
TEL;HOME;FAX:+1 (818) 555-1222
TEL;HOME:+1 (818) 555-1221
TEL;ISDN:+1 (818) 555-1223
TEL;PREF:+1 (818) 555-1227
ADR;WORK:po box;office;123 street;city;CA;91040;United States of America
LABEL;WORK;ENCODING=3DQUOTED-PRINTABLE:office=3D0D=3D0Apo =
box=3D0D=3D0A123 street=3D0D=3D0Acity, CA 91040=3D0D=3D0AUnited States =
of=3D
 America
ADR;HOME:home po box;;123 street;city home;CA;91040;United States of =
America
LABEL;HOME;ENCODING=3DQUOTED-PRINTABLE:home po box=3D0D=3D0A123 =
street=3D0D=3D0Acity home, CA 91040=3D0D=3D0AUnited States of A=3D
merica
ADR;POSTAL:other po box;;123 street;city other;CA;91040;United States of =
America
LABEL;POSTAL;ENCODING=3DQUOTED-PRINTABLE:other po box=3D0D=3D0A123 =
street=3D0D=3D0Acity other, CA 91040=3D0D=3D0AUnited States of=3D
 America
URL;HOME:http://www.personal.com
URL;WORK:http://www.web.com
ROLE:profession
BDAY:19520411
EMAIL;PREF;INTERNET:nr1@email.com
EMAIL;TLX:+1 (818) 555-1229
FBURL;ENCODING=3DQUOTED-PRINTABLE:????????????y0y =3D05=3D06C=3D08?g
REV:20060327T063456Z
END:VCARD


Reply With Quote
  #8 (permalink)  
Old March 29th, 2006, 06:25 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm sorry, I can't see the relationship between the input and the output. They seem to be using completely unrelated datasets.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #9 (permalink)  
Old March 29th, 2006, 06:50 PM
Registered User
 
Join Date: Mar 2006
Location: Sunland, California, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, you are right - this was only intended as a sample for the vCard format.
But here is a simple data in and out from one simple data set.

Thanks
Gunter


===== in ===========================
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="allad.xsl"?>

<ALLAD>
  <MARK>
    <LETTER>====L====</LETTER>

    <ADD>
      <FN>Firsname</FN>
      <LN>Lastname</LN>
      <HS>1234 Street Court</HS>
      <HC>Burbank</HC>
      <HT>California</HT>
      <HZ>91504</HZ>
      <PH>(818) 555-4962</PH>
      <MB>(818) 555-2870</MB>
      <EM>name@mail.net</EM>
      <NO>this is a test note - #1</NO>
      <NO>this is a test note - #2</NO>
    </ADD>

  </MARK>

</ALLAD>

===== out ==========================
BEGIN:VCARD
VERSION:2.1
N:Lastname;Firsname;;;
FN:Firsname Lastname

ADR;HOME:;;1234 Street Court;Burbank;California;91504;
LABEL;HOME;ENCODING=QUOTED-PRINTABLE:1234 Street Court=0D=0ABurbank, California91504=0D=0A
TEL;HOME;VOICE:(818) 555-4962
TEL;CELL;VOICE:(818) 555-2870
EMAIL;PREF;INTERNET:name@mail.net
NOTE;ENCODING=QUOTED-PRINTABLE:this is a test note - #1
NOTE;ENCODING=QUOTED-PRINTABLE:this is a test note - #2
END:VCARD


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:param and xsl:apply-templates' "select" newbieboobers XSLT 1 March 25th, 2008 07:23 PM
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
differnce between xsl:apply-templates and xsl:call chandu.mca007 XSLT 2 June 12th, 2007 04:12 AM
xsl advise needed - Replacing UID through XSL Billyl XSLT 6 February 28th, 2006 05:46 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM



All times are GMT -4. The time now is 09:40 PM.


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