0

I am trying to use the < sub > < /sub > tag to make parts of my xml file subscripts when opened online but there is an issue when I open up the xml file in Internet Explorer (doesn't say what error just does not look right in IE). I think it is because xml is reading < sub > as another child element but < sub > is just supposed to be used to tell hmtl to make certain parts of the xml file subscripts online. Any ideas on what to change? I am also using xsl to convert xml to html for online publication. The code for that is below

XML File:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="ALDformat.xsl"?>

<ALD_data>
<Element Title = "lithium">
<Element>lithium </Element>
<Compound Subtitle = "Li<sub>2</sub>CO<sub>3</sub>">
<Compound> Li<sub>2</sub>CO<sub>3</sub> </Compound>
<Precursor>
<precursor1> Li(thd) </precursor1>
<precursor2> O<sub>3</sub> </precursor2>
<Ref2> Putkonen2009 </Ref2>
</Precursor>
</Compound>
<Compound Subtitle = "Li<sub>2</sub>O(LiOH)">
<Compound> Li<sub>2</sub>O(LiOH) </Compound>
<Precursor>
<precursor1> Li(O<sup>t</sup>Bu) </precursor1>
<precursor2> H<sub>2</sub>O </precursor2>
<Ref2> Aaltonen2010 </Ref2>
</Precursor>
</Compound>
</Element>
</ALD_data>

XSL File:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">

<h1>ALD literature XML database</h1>
<p>Last update: 6 January 2016</p>

<xsl:for-each select="ALDdata/Element">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"> Element: <xsl:value-of select="Element"/> </span>
  </div>
  <xsl:for-each select="Compound">
    <div style="background-color:lightgrey;margin-left:20px;margin-bottom:1em;font-size:10pt">
      <p>
      <span> Compound: <xsl:value-of select="Compound"/> </span>
      </p>
    </div>
    <xsl:for-each select="Precursor">
      <div style="margin-left:30px;margin-bottom:1em;font-size:10pt">
        <p>
        Precursor 1: <xsl:value-of select="precursor1"/> <br/>
        Precursor 2: <xsl:value-of select="precursor2"/>
        </p>
      Post-2005 review paper references
      <ol>
      <xsl:for-each select="Ref2">
          <li><xsl:value-of select="."/></li>
      </xsl:for-each>
      </ol>
      </div>
    </xsl:for-each>
  </xsl:for-each>
</xsl:for-each>
</body>
</html>
Zach Thomas
  • 147
  • 12
  • 1) You should use UTF-8 for encoding with XML. 2) You need to transform the XML to HTML: [Converting XML to HTML using XSL](http://www.htmlgoodies.com/beyond/xml/converting-xml-to-html-using-xsl.html) and, possibly outdated for your version of IE: [Hello, World! (XSLT)](https://msdn.microsoft.com/en-us/library/ms765388(v=vs.85).aspx), [Initiate XSLT in a Script](https://msdn.microsoft.com/en-us/library/ms762796(v=vs.85).aspx). – Andrew Morton Jan 06 '17 at 18:54
  • @Andrew Morton I updated the code to include my xsl code. However, I still run into the issue of the when publishing the xml code online because it reads those tags as separate nodes. Any suggestions? – Zach Thomas Jan 10 '17 at 17:44
  • Do you have any control of the XML file? E.g. you are the creator of it. – Andrew Morton Jan 10 '17 at 19:52
  • I am the creator of it. – Zach Thomas Jan 10 '17 at 20:50
  • In that case, you might want to review the structure of the XML. Perhaps an "" could have sub-elements like "", and "" could be "" with sub-elements "" without numbers so that you are not limited to two of them, and so on. Maybe using `<![CDATA[]]>` ( see http://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean ) for the verbatim HTML would make life easier too. – Andrew Morton Jan 10 '17 at 21:16

2 Answers2

2

In order for your sub and sup elements to be processed, you'll have to use xsl:apply-templates instead of xsl:value-of for the elements that contain sub and sup.

I don't think you can use a literal result element as a stylesheet and add templates.

Maybe try something like this instead...

XML Input (Note: I had to remove the values of the Compound/@Subtitle attributes as they are not well-formed.)

<?xml-stylesheet type="text/xsl" href="ALDformat.xsl"?>
<ALD_data>
    <Element Title = "lithium">
        <Element>lithium </Element>
        <Compound Subtitle = "">
            <Compound> Li<sub>2</sub>CO<sub>3</sub> </Compound>
            <Precursor>
                <precursor1> Li(thd) </precursor1>
                <precursor2> O<sub>3</sub> </precursor2>
                <Ref2> Putkonen2009 </Ref2>
            </Precursor>
        </Compound>
        <Compound Subtitle = "">
            <Compound> Li<sub>2</sub>O(LiOH) </Compound>
            <Precursor>
                <precursor1> Li(O<sup>t</sup>Bu) </precursor1>
                <precursor2> H<sub>2</sub>O </precursor2>
                <Ref2> Aaltonen2010 </Ref2>
            </Precursor>
        </Compound>
    </Element>
</ALD_data>

XSLT 1.0 (ALDformat.xsl)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
    <html>
      <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
        <h1>ALD literature XML database</h1>
        <p>Last update: 6 January 2016</p>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Element[@Title]">
    <div style="background-color:teal;color:white;padding:4px">
      <span style="font-weight:bold"> Element: <xsl:apply-templates select="Element"/> </span>
    </div>
    <xsl:apply-templates select="Compound"/>
  </xsl:template>

  <xsl:template match="Compound[@Subtitle]">
    <div style="background-color:lightgrey;margin-left:20px;margin-bottom:1em;font-size:10pt">
      <p>
        <span> Compound: <xsl:apply-templates select="Compound"/> </span>
      </p>
    </div>
    <xsl:apply-templates select="Precursor"/>      
  </xsl:template>

  <xsl:template match="Precursor">
    <div style="margin-left:30px;margin-bottom:1em;font-size:10pt">
      <p>
        Precursor 1: <xsl:apply-templates select="precursor1"/> <br/>
        Precursor 2: <xsl:apply-templates select="precursor2"/>
      </p>
      Post-2005 review paper references
      <ol>
        <xsl:apply-templates select="Ref2"/>
      </ol>
    </div>      
  </xsl:template>

  <xsl:template match="sub|sup">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="Ref2">
    <li><xsl:apply-templates/></li>
  </xsl:template>

</xsl:stylesheet>

Output

<html>
   <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
      <h1>ALD literature XML database</h1>
      <p>Last update: 6 January 2016</p>
      <div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold"> Element: lithium </span></div>
      <div style="background-color:lightgrey;margin-left:20px;margin-bottom:1em;font-size:10pt">
         <p><span> Compound:  Li<sub>2</sub>CO<sub>3</sub></span></p>
      </div>
      <div style="margin-left:30px;margin-bottom:1em;font-size:10pt">
         <p>
            Precursor 1:  Li(thd) <br>
            Precursor 2:  O<sub>3</sub></p>
         Post-2005 review paper references
         
         <ol>
            <li> Putkonen2009 </li>
         </ol>
      </div>
      <div style="background-color:lightgrey;margin-left:20px;margin-bottom:1em;font-size:10pt">
         <p><span> Compound:  Li<sub>2</sub>O(LiOH) </span></p>
      </div>
      <div style="margin-left:30px;margin-bottom:1em;font-size:10pt">
         <p>
            Precursor 1:  Li(O<sup>t</sup>Bu) <br>
            Precursor 2:  H<sub>2</sub>O 
         </p>
         Post-2005 review paper references
         
         <ol>
            <li> Aaltonen2010 </li>
         </ol>
      </div>
   </body>
</html>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
0

You can't place HTML in a stylesheet right away, your XSLT must have the stylesheet element at the top level, and have template elements as child content, like

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
      <html>
         <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">

             <h1>ALD literature XML database</h1>
             <p>Last update: 6 January 2016</p>

             <xsl:for-each select="ALDdata/Element">
                 <!-- ... -->
             </xsl:for-each>
         </body>
      </html>
  </xsl:template>
</xsl:stylesheet>

I must admit, it's been a while since I used in-browser XSLT. Are you using IE by chance?

imhotap
  • 2,275
  • 1
  • 8
  • 16
  • I am using IE. And when I run my xsl code that I posted it works with the xml file I have without the tags but not with them in the xml document. – Zach Thomas Jan 10 '17 at 18:59
  • @ZachThomas - You're going to need to use `xsl:apply-templates` instead of `xsl:value-of`. You're not going to be able to do that using a literal result element as a stylesheet though (as far as I know; I never write XSLT that way). – Daniel Haley Jan 10 '17 at 19:08