This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Using XSL for Serialization with Javascript.


> Here's an example of what I mean. Lets say that I have a mailinglist
object
> that is a collection of contact objects. We might create such a object in
> Javascript with something like:
>
> function setup() {
> var list = new MailingList();
> var aContact
> for(var x = 0; x<20; x++) {
> aContact = new Contact();
> aContact.name = "Someone";
> aContact.email = "someone@someone.com";
> list.addContact(aContact);
> }
> }

(Note: I have not tried the code below)
you could use XSL to write out the javascript object

the XML:
<people>
<person>
    <name>Someone</name>
    <email>Someone@someone.com</email>
</person>

<person>
    <name>Sometwo</name>
    <email>Sometwo@someone.com</email>
</person>

<person>
    <name>Somethree</name>
    <email>Somethree@someone.com</email>
</person>
</people>

the XSL:

<xsl:template match="people">
    &lt;script&gt;
        var list = new MailingList();
        var aContact
        <xsl:apply-templates/>
        list.addContact(aContact);
    &lt;/script&gt;
</xsl:template>

<xsl:template match="person">
    <xsl:variable name="i" select="position()-1"/>
    aContact[$i].name = "<xsl:value-of select="name"/>";
    aContact[$i].email = ""<xsl:value-of select="email"/>";
</xsl:template>

the result could look like:

var list = new MailingList();
var aContact

aContact[0].name = "Someone";
aContact[0].email = "mailto:someone@someone.com;
aContact[1].name = "Sometwo";
aContact[1].email = "mailto:sometwo@someone.com;
aContact[2].name = "Somethree";
aContact[2].email = "mailto:somethree@someone.com;
list.addContact(aContact);



hth



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]