Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries [Thread Prev] >Thread Next - Re: xslt different levels xslt different levelsTo: NULL Date: 1/7/2008 2:52:00 PM
If I have the following xml:
<?xml version="1.0"?>
<addresses>
<address country="Italy">
<tag name="street" flag="5">One Microsoft Way</tag>
<tag name="number">1</tag>
<tag name="city">Redmond</tag>
<tag name="state" unit="25">WA</tag>
<tag name="zip">98052</tag>
</address>
<address country="France">
<section type="Condo" Comp="1">
<tag name="street">15 Mako Place</tag>
<tag name="number">1545</tag>
<tag name="city">Fargo</tag>
<tag name="state" unit="10">Paris</tag>
<tag name="zip">10110</tag>
</section>
</address>
</addresses>
Where one section is addresses/address/tag and another is
addresses/address/section/tag, I want to be able to get the country from the
address.
Nomrally I would just do
<xsl:value-of select="ancestor::address/@country"/>
But if I have a "section", I would need to go up 2 levels.
My current xsl is:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="addresses">
<dataset>
<xsl:apply-templates/>
</dataset>
</xsl:template>
<xsl:template match="tag|section/tag">
<address>
<sectionNumber>
<xsl:value-of select="ancestor::section/@Comp"/>
</sectionNumber>
<name>
<xsl:value-of select="@name"/>
</name>
<flag>
<xsl:if test="not(@flag)">
<xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:if>
<xsl:value-of select="@flag"/>
</flag>
<unit>
<xsl:if test="not(@unit)">
<xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:if>
<xsl:value-of select="@unit"/>
</unit>
<value>
<xsl:value-of select="."/>
</value>
</address>
</xsl:template>
</xsl:stylesheet>
I would like to add a section:
<country>
<xsl:value-of select="ancestor::address/@country"/>
</country>
but this won't work for both cases.
I could set up 2 template sections, but I would prefer to do this with some
sort of if statement but am not sure how to set it up.
Also, do I need the if test="not(@unit)" test in the following code? If I
don't have it, it still seems to work.
<unit>
<xsl:if test="not(@unit)">
<xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:if>
<xsl:value-of select="@unit"/>
</unit>
Thanks,
Tom
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
