0 Replies Latest reply on Sep 9, 2015 3:21 PM by Editrix

    Styling a nested XML element with XSLT

    Editrix

      I’m a designer working on a travel guide that we recently started managing with XML-tagged content and XSLT styling. It mostly works, aside from this one small issue that has driven us to wit’s end! We have some attraction listings that should appear as “child” listings that we can style differently in InDesign, and they’re noted in the XML by listing their “parent” attraction in the <MainAttraction> tag.

       

      My understanding is that we need the .XSL to notice whether there’s a value in the <MainAttraction> tags, and if there is, then to re-tag all the fields associated with that attraction so we can style them differently. I just haven’t had any luck writing syntax for this that works after doing some basic training and Googling around forums.

       

      Here’s an example of the XML structure we have to work with—in this case, SUPER SECRET PLACE would be a child listing that we’d want to list right after ANOTHER PLACE, but styled differently:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <Root>
      <City>
          <City_Name>…</City_Name>
                <Attraction>
                    <Attraction_Title>SOME PLACE</Attraction_Title>
                    <Desc>… </Desc>
                    <Admissions>…</Admissions>
                    <Address>…</Address>
                    <Directions></Directions>
                    <Contact>…</Contact>
                    <MainAttraction></MainAttraction>
                </Attraction>    
                <Attraction>
                    <Attraction_Title>ANOTHER PLACE</Attraction_Title>          
                    <Desc>… </Desc>
                    <Admissions>…</Admissions>
                    <Address>…</Address>
                    <Directions></Directions>
                    <Contact>…</Contact>
                    <MainAttraction></MainAttraction>
                </Attraction>
                <Attraction>
                    <Attraction_Title>SUPER SECRET PLACE</Attraction_Title>
                    <Desc>… </Desc>
                    <Admissions>…</Admissions>
                    <Address>…</Address>
                    <Directions></Directions>
                    <Contact>…</Contact>    
                    <MainAttraction>ANOTHER PLACE</MainAttraction>
                </Attraction>
      </City>
      </Root>
      
      
      
      

       

       

      And here is our .XSL for the attractions section:

       

      <?xml version="1.0" ?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      
         <xsl:template match="/">
      
         <Cities>
         <xsl:for-each select="Root/City">
         <City>
         <!-- I cut some stuff out of here -->
         </City>
      
         </xsl:for-each>
      
         </Cities>
      
         </xsl:template>
      
      
         <xsl:template match="Attraction">
      
                             <Attraction>
      
                                                                  <Attraction_Title>
                                                                              <xsl:value-of select="normalize-space(Attraction_Title)"/>
                                                                  </Attraction_Title>
                                                                  <xsl:text>&#8212;</xsl:text>
      
                                                                  <xsl:value-of select="Desc"/><xsl:text> </xsl:text>
      
                                                                  <xsl:value-of select="normalize-space(Admissions)"/>
                                                                              <xsl:if test="string-length(Admissions) &gt; 0">
                                                                                          <xsl:text>. </xsl:text>
                                                                              </xsl:if>
      
                                                                  <xsl:value-of select="normalize-space(Address)"/>
                                                                  <xsl:if test="string-length(Address) &gt; 0">
                                                                              <xsl:text>. </xsl:text>
                                                                  </xsl:if>
      
                                                      <xsl:value-of select="normalize-space(Directions)"/>
                                                      <xsl:if test="string-length(Directions) &gt; 0">
                                                                  <xsl:text>. </xsl:text>
                                                      </xsl:if>
      
                                                       <xsl:if test="string-length(Contact) &gt; 0">
                                                                  <xsl:text>. </xsl:text>
                                                      </xsl:if>
      
                                                      <xsl:text>&#xa;</xsl:text>
                            
      
      
      
      
      
      </Attraction>
      
      
      
      
      
      
      
      
      
      </xsl:template>
      
      
      
      
      </xsl:stylesheet>
      
      
      

       

      I can provide actual working files if that helps...I have only basic knowledge of how all this works and would be so grateful if someone could offer working code to plug in! I can provide actual working files, if needed.

       

      Thanks a bunch!