Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


xslt different levels

From: "tshad" <tshad@----------.--->
To: 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 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent