html subscript in XML

时间:2017-01-06 18:02:01

标签: html xml xslt

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>

2 个答案:

答案 0 :(得分:2)

为了处理您的subsup元素,您必须使用xsl:apply-templates代替xsl:value-of来处理包含sub的元素}和sup

我认为您不能使用literal result element as a stylesheet并添加模板。

也许尝试这样的事情......

XML输入(注意:我必须删除Compound / @ Subtitle属性的值,因为它们不是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>

<强>输出

<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>

答案 1 :(得分:0)

您不能立即将HTML放在样式表中,您的XSLT必须在顶层具有stylesheet元素,并且template个元素作为子内容,例如

<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>

我必须承认,自从我使用浏览器内的XSLT以来已经有一段时间了。你偶然使用IE浏览器吗?