Altova Mailing List Archives


Re: [xml-dev] XSTL stylesheet workarounds for exceptions

From: Jack Bush <netbeansfan@-----.---.-->
To: Michael Kay <mike@--------.--->, xml-dev@-----.---.---
Date: 2/3/2009 1:14:00 PM
Hi Michael,

Thanks for your suggestion which I have tried out without success still.

I can confirm that the namespace "ns" is correct and that the template I am applying to did pick up the correct <p>, where many other data including Hobbies (those with only a single child <a>) have been successfully extracted. However, there is only one Hobbies record with 2 child <a>s which may be the reason why XPath is not able to navigate pass both of them.

My next step would be to debug this stylesheet but any other suggestion would be more than welcome.

Thanks again,

Jack


________________________________
From: Michael Kay <mike@s...>
To: Jack Bush <netbeansfan@y...>; xml-dev@l...
Sent: Tuesday, 3 February, 2009 8:39:43 AM
Subject: RE: [xml-dev] XSTL stylesheet workarounds for exceptions


You would really be much better off asking these questions on xsl-list at mulberrytech.com, especially at times when xml-dev is descending down philosophical rat-holes, which means the practical guys are hitting the delete button...
 
Most of your attempts look correct, assuming you have got the namespace right. They are all more complicated than they need to be; the following would do fine
 
    <xsl:template match="ns:p[ns:strong='Hobbies:']"> 
         <xsl:for-each select="ns:a"> 
           <hobbies><xsl:value-of select="."/></hobbies> 
         </xsl:for-each> 
       </xsl:template>
If that doesn't work, there are two explanations: either you've got the namespace declaration for "ns" wrong, or you aren't applying templates to the relevant <p> element. Either of those explanations would stop all the other attempts working as well. (It's a common story - people show us the code where they think the bug is, and all this reveals is that they are looking in the wrong place.)
Michael Kay
http://www.saxonica.com/
 
 


________________________________
From: Jack Bush [mailto:netbeansfan@y...] 
Sent: 02 February 2009 21:04
To: xml-dev@l...
Subject: [xml-dev] XSTL stylesheet workarounds for exceptions


Hi All,
 
I am at a stage of fine-tuning an existing stylesheet that has been worked on recently and would like include some workarounds to accommodate exceptional data in the following 2 areas: 
 
( i ) Get the content of sub-elements <a> as follows:
<p>
  <strong>Hobbies:</strong> 
  <a shape="rect" href=http://www.hobbies.com/fishing title="Shark Hunting">fishing</a> 
  <a shape="rect" href=http://www.hobbies.com/hunting title="Animal">rabbit</a> 
(Out-door adventures)
</p>
 Intended output
<hobbies>fishing<hobbies>
<hobbies>rabbit<hobbies>
 
In this case, it is both fishing, rabbit that I am after. However, I still couldn’t get it working despite having tried numerous of the following stylesheet approaches: 
 
<xsl:template match="/">
  <employee>
    <xsl:apply-templates select="//ns:p"/>
  </employee>
</xsl:template>
 
( A ) <xsl:template match="ns:p[ns:strong='Hobbies:']">          <xsl:copy-of              
           <hobbies>select="ns:a"/></hobbies> 
       </xsl:template> 
               or
( B ) <xsl:template match="ns:p[ns:strong='Hobbies:']">
         <xsl:for-each select="ns:a/text()[normalize-space() != '']"> 
           <hobbies><xsl:value-of select="normalize-space()"/></hobbies> 
         </xsl:for-each> 
       </xsl:template> 
               or
( C ) <xsl:template match="ns:p[ns:strong='Hobbies:']"> 
         <xsl:for-each select=".//ns:a/text()[normalize-space() != '']"> 
           <hobbies><xsl:value-of select="normalize-space()"/></hobbies> 
         </xsl:for-each> 
       </xsl:template>
              or
( D ) <xsl:template match="ns:p[ns:strong='Hobbies:']"> 
         <xsl:for-each select="ns:p/ns:a/text()[normalize-space() != '']"> 
           <hobbies><xsl:value-of select="normalize-space()"/></hobbies> 
         </xsl:for-each> 
       </xsl:template>
               or
( E ) <xsl:template match="ns:p[ns:strong='Hobbies:']"> 
         <xsl:for-each select="../ns:a//text()[normalize-space() != '']">
           <hobbies><xsl:value-of select="normalize-space()"/></hobbies> 
         </xsl:for-each> 
       </xsl:template>
               or
( F ) <xsl:template match="ns:p[ns:strong='Hobbies:']">          <xsl:for-each select="ns:a">
          <xsl:for-each select="text()[normalize-space() != '']">"
            <hobbies><xsl:value-of select="normalize-space()"/></hobbies> 
          </xsl:for-each> 
        </xsl:for-each> 
     </xsl:template>
               or 
( G ) <xsl:template match="ns:p[ns:strong='Hobbies:']">           <xsl:for-each select=".//ns:a">
            <xsl:for-each select="text()[normalize-space() != '']">"
              <hobbies><xsl:value-of select="normalize-space()"/></hobbies> 
            </xsl:for-each> 
          </xsl:for-each>
        </xsl:template>
 
( ii ) I also like to add a default generic value for any missing element. Say if there are no <p><strong>Hobbies:</strong></p>
null element such as <hobbies>Unknown</hobbies>. The intended stylesheet should look 
like the following but again couldn’t get it working still:
 
<xsl:template match="/">
  <employee>
    <xsl:apply-templates select="//ns:p"/>
  </employee>
</xsl:template>
 <xsl: choose>
  <xsl: when test="ns:p[ns:strong='Hobbies:']">
    <xsl:template match="ns:p[ns:strong='Hobbies:']"> 
      <xsl:for-each select="text()[normalize-space() != '']"> 
        <hobbies><xsl:value-of select="normalize-space()"/></hobbies> 
      </xsl:for-each> 
    </xsl:template>  </xsl:when>
<xsl:otherwise>
  <hobbies>Unknown</hobbies> 
</xsl:otherwise>
</xsl: choose>
 
I have run out of ideas and would be very much appreciated if anyone could give me some suggestion on how to tackle this task.
Jack

 element in such document, yet I still would like to generate a 
________________________________
Stay connected to the people that matter most with a smarter inbox. Take a look.


      Make Yahoo!7 your homepage and win a trip to the Quiksilver Pro. Find out more

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.