Click to See Complete Forum and Search --> : Xslt


zoltan_ie
October 2nd, 2003, 07:12 AM
I'm total new to XSLT and I've just about given up looking on the web for a decent tutorial.


<source>
<instance>
<fragment label="1">42</fragment>
<fragment label="2">THE</fragment>
<fragment label="3">HEATH</fragment>
<fragment label="4">CYPRESS</fragment>
<fragment label="5">DOWNS</fragment>
<fragment label="6">DUBLIN</fragment>
<fragment label="7">6</fragment>
<fragment label="8">DUBLIN</fragment>
</instance>
<instance>
<fragment label="9">39</fragment>
<fragment label="10">KILCARN</fragment>
<fragment label="11">COURT</fragment>
<fragment label="12">NAVAN</fragment>
<fragment label="13">MEATH</fragment>
</instance>
</source>



<source>
<instance>
<organisation_name>42</organisation_name>
<sub_building>THE</sub_building>
<building_name>HEATH</building_name>
<building_number>CYPRESS</building_number>
<street>DOWNS</street>
<street_pre>DUBLIN</street_pre>
<street_body>6</street_body>
<street_post>DUBLIN</street_post>
</instance>
<instance>
<locality>39</locality>
<county>KILCARN</county>
<county_area_subcode>COURT</county_area_subcode>
<country>NAVAN</country>
<invalid>MEATH</invalid>
</instance>
</source>



where
1 = organisation_name
2 = sub_building
3 = building_name
4 = building_number
5 = street
6 = street_pre
7 = street_body
8 = street_post
9 = locality
10 = county
11 = county_area_subcode
12 = country
13 = invalid


My lame attempt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">

<xsl:for-each select="//instance">

<xsl:for-each select="//fragment">
<xsl:if match="1 = @label">
<xsl:text>
<organisation_name>
<xsl:value-of select="//fragment"/>
</organisation_name>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>



can any one help me?

or can anyone tell me to use xsl:if or any of the other conditional statements properly. As in how to check string values agains another string value.


These are the only good sites I found but had no luck with them
http://www.zvon.org/xxl/XSLTutorial/Books/Output/index.html

http://www.w3schools.com/xsl/default.asp

Does any one know any other good ones?

khp
October 2nd, 2003, 10:06 AM
Your if statement is almost correct, only problem is that the attribute for the evaluation must be called test rather than match.

I have taken the liberty of rewriting your code a bit, to avoid those ugly globally scoped selections, which dosn't quite do what you expect.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<xsl:apply-templates select="source"/>
</xsl:template>

<xsl:template match="source">
<source>
<xsl:apply-templates select="instance"/>
</source>
</xsl:template>

<xsl:template match="instance">
<instance>
<xsl:apply-templates select="fragment"/>
</instance>
</xsl:template>

<xsl:template match="fragment">
<xsl:if test="1 = @label">
<organisation_name>
<xsl:value-of select="text()"/>
</organisation_name>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

zoltan_ie
October 2nd, 2003, 10:09 AM
Cheers.

I'd like to learn more but the tutorial sites that I have found seem to be useless.

Is there any good tutorial sites that you know out there that would be useful? Or do I need to get a book?

khp
October 2nd, 2003, 10:12 AM
Tutorials, no sorry, don't know any tutorials.
I learned xslt by reading the specifications.

http://www.w3.org/TR/xslt

Which IMHO are quite good, and resonably easy to read.

zoltan_ie
October 2nd, 2003, 11:07 AM
What I don't get is how to save the translated xml?

in the xml I want to translate I have this
<?xml-stylesheet type="text/xsl" href="C:\Translator.xslt"?>

When I open internet explorer I see the translated page but when I look at the source code all I see is the original. How do I save the output?

khp
October 2nd, 2003, 11:21 AM
Originally posted by zoltan_ie
When I open internet explorer I see the translated page but when I look at the source code all I see is the original.

Yes the translation is preformed by the browser, and the browser always shows, the original root source document, when you select 'view source'. The same goes for javascript pages and pages that use frames.

To save the result of the xslt transformation, I suppose you could 'copy and paste' from the browser window (if you just need it this one time). Otherwise you should use another xslt processor, like xalan (http://xml.apache.org/xalan-j/index.html) which can be controlled via the command line.