Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - xslt different levels >Thread Next - Re: xslt different levels Re: xslt different levelsTo: NULL Date: 1/8/2008 11:28:00 AM "tshad" <tshad@d...> wrote in message news:eu9R3$XUIHA.5816@T...... > 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. > Why not? I think you are confusing ancestor:: with parent:: . The current tag node whether it be a child of section or address will have an ancestor of address. -- Anthony Jones - MVP ASP/ASP.NET | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
