Fog Creek Software
Discussion Board




XSLT Question

I've got this:
<Address>
    <Address1>123 2nd Street</Address1>
    <Address2>Suite 123</Address2>
</Address>

Which I want to produce this:
N3*123 2nd Street*Suite 123^

Easy enough, BUT, I also need this:
<Address>
    <Address1>123 2nd Street</Address1>
</Address>

Which I want to produce this:
N3*123 2nd Street*^ 

AND

<Address>
    <Address2>Suite 123</Address2>
</Address>

Which I want to produce this:
N3**Suite 123^

(note all separators present)

But if the node isn't there, then I don't want the line rendered at all. In other words, if any data element is there, then I need all the structure in place, but if the node isn't there, then no line.

Can XSLT get me there? Or am I gonna end up tearing my hair out?

Philo

Philo
Monday, June 23, 2003

Yeah, it's not too hard.
You can do it 'proceduraly' (xsl:if one of them is present, then two xsl:value-of statements with all your punctuation).

Doing it as fully 'functionally' is more difficult but possible.

mb
Monday, June 23, 2003

and if you mean 'neither of them there' you mean no address node at all, you can easily do it 'functionally'.

mb
Monday, June 23, 2003

<xsl:template match="Address">
N3*<xsl:value-of select="./Address1" />*<xsl:value-of select="./Address2" />
</xsl:template>

Tom Vu
Monday, June 23, 2003

The above works using AxKit modperl; it should work using anything but I am not sure if hardcoding * and N3 is valid.

Tom Vu
Monday, June 23, 2003

<xsl:template match="Address">
N3*<xsl:value-of select="Address1" />*<xsl:value-of select="Address2" />^
</xsl:template>

Absolutely valid. No need to re-match on '.'. You can use xsl:text to try and deal with white-space, that's going to be the biggest issue.

mb
Monday, June 23, 2003

Philo,

Not answering your specific question (I'm JOSing to avoid an XSLT problem I'm having myself and not wanting to think in XSLT right now), if you over to http://www.zvon.org they have a very good XSLT reference and tutorial.

Walter Rumsby
Monday, June 23, 2003

Actually I'm using the stylesheet designer that came with XML Spy, and it's been great! I laid out about 28k worth of XSLT in a few hours - just need to do a little hand tweaking and I'm done.

But thanks for the reference - I'll still need it!

Philo

Philo
Tuesday, June 24, 2003

XML Spy ROCKS.

www.MarkTAW.com
Tuesday, June 24, 2003

Philo, spend an hour or so looking at

http://www.vbxml.com/xsl/tutorials/intro/

also try

http://www.jenitennison.com/

Justin
Tuesday, June 24, 2003

In my experience, XML Spy is absolute crap.

It's a really good example of why Java/Swing makes for a bad desktop application. Plus its xml schema validation is very questionable. I've often hand-coded xml's and schema's using the w3c documentation and they fail to validate using XML Spy. Yet they happily pass using the MS XML parser (which is also about 10 times faster). And before anyone asks, I'm not using any MS specific "enhancements".

This refers to version 4.2, perhaps they've lifted their game?

Anon
Tuesday, June 24, 2003

<offtopic>
I'm pretty sure XMLSpy is not Java, Swing, or anything even closely related.  The first clue is the UI -- it's XP compliant, which is something that you don't get in a Java app unless you're running the L&F from JGoodies.com, which they're not.  Second, you can integrate the MSXML parser.  Third, no jars or zips floating around in the installation, just a pile of dll's.  *Not* java.
</offtopic>

Chas
Tuesday, June 24, 2003

On topic - this seems to work

<xsl:for-each select="Address">
    <xsl:if test="count(*) &gt; 0">
        N3*<xsl:value-of select="Address1"/>*<xsl:value-of select="Address2"/>^
    </xsl:if>
</xsl:for-each>

There's about a billion ways to count the number of children that an element has, so you may have come up with something else by now.

Not Java? - To be honest, I just assumed because it was so slow. I'll have to investigate.

Anon
Tuesday, June 24, 2003

> N3**Suite 123^

Gee, that looks like ANSI X12.  HIPAA project?

Matt Conrad
Tuesday, June 24, 2003

Anon--that will work for some scenarios, but the template match is much better. Or you could match on text="address1|address2".

What happens with your template and the following XML:

<address>
<email>example@example.com</email>
</address>

XSLT is really cool, the key when writing them by hand is to think declaratively first, then fall back to procedural mechanisms when it gets too complex.

GUI based tools tend to use big templates with lots of 'procedural' ideas because they're easier to handle.

mb
Tuesday, June 24, 2003

s/text/test/

mb
Tuesday, June 24, 2003

Matt - no, it's an EDI purchase order/Invoicing engine. It is X12 tho. My baby, which is over 50k documents processed.

Philo

Philo
Tuesday, June 24, 2003

EDI. Finally, someone is doing EDI work with XML. I did bisac purchase orders and acknowledgments years ago. Parsed them in C as required by the boss.

Tom Vu
Wednesday, June 25, 2003

mb

Point taken, but I was answering the problem specified in the original post. He didn't mention that there might be different formats for that node within the document. Since he's a programmer, I took him at his word. If he were a client then I would have made a more general and safer solution.

As for XML Spy, I downloaded and reviewed version 5 and must adjust my opinion accordingly.

XML Spy version 4.2 - completely sucks.

XML Spy version 5 - very nice.

Also it does seem to be written in c++, but they must have written their own gui widgets (or bought some from a third party). Just goes to show that you can write a slow desktop app in any language.

anon
Wednesday, June 25, 2003

For info, we found Sonic Stylus Studio easier to use than XML Spy to develop with XSLT.

YMMV

cf http://www.sonicsoftware.com/products/additional_software/stylus_studio/index.ssp

R Chevallier
Wednesday, June 25, 2003

Congratulations.  That is impressive.  I have done a good deal of work with some quasi-EDI processing using our vendor's software (gag), and am only now getting into writing some from scratch.

It's a little harder than I expected.  I think this is actually a case where my lack of formal education is cramping me a bit.  What I've done works, but it is ugly.  I can't help feeling there is a more clever approach.

[non-seq] Damn, what's up with all the new topics?  I'm not going to be able to keep up at this rate.

Matt Conrad
Thursday, June 26, 2003

"It's a little harder than I expected.  I think this is actually a case where my lack of formal education is cramping me a bit.  What I've done works, but it is ugly.  I can't help feeling there is a more clever approach."

No, there isn't. Trust me - if it works, it's clever. :-)

The most interesting thing I've discovered about EDI is that the mastery isn't in knowing the specifications; it's in accepting that nobody follows them.

Philo

Philo
Thursday, June 26, 2003

*  Recent Topics

*  Fog Creek Home