我如何解决此xml和xsl代码,因为它不起作用

时间:2019-11-13 03:39:05

标签: html xml xslt

第一次尝试对xml进行编码并将xsl用于html输出。

我采用了W3示例,并根据需要对其进行了修改,但是由于在浏览器中查看xml可以正常工作,所以我错过了一些东西。没有html代码显示在浏览器中,但是xml中的项目信息显示为长列表。

浏览器中显示的内容:

SCALARADD Image1inSCALARADD Add one value to an image. A B A same as B allowed redLevel, greenLevel, blueLevel TRIMFILL Image1inTRIMFILL Trim an image and fill the trim area. A B A same as B allowed trimTopIndex, trimBottomIndex, trimLeftIndex, trimRightIndex fillRedLevel, fillGreenLevel, fillBlueLevel

这是xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="routines.xsl"?>

<family>
  <Image1in>
    <routine>SCALARADD</routine>
    <symbolic> Image1inSCALARADD </symbolic>
    <operation> Add one value to an image. </operation>
    <inputLayer1> A </inputLayer1>
    <inputLayer2></inputLayer2>
    <outputLayer1> B </outputLayer1>
    <commentLayer> A same as B allowed </commentLayer>
    <inputLong></inputLong>
    <inputDouble> redLevel, greenLevel, blueLevel </inputDouble>
    <outputLong></outputLong>
    <outputDouble></outputDouble>
    <comment>  </comment>
  </Image1in>

  <Image1in>
    <routine>TRIMFILL</routine>
    <symbolic> Image1inTRIMFILL </symbolic>
    <operation> Trim an image and fill the trim area. </operation>
    <inputLayer1> A </inputLayer1>
    <inputLayer2></inputLayer2>
    <outputLayer1> B </outputLayer1>
    <commentLayer> A same as B allowed </commentLayer>
    <inputLong> trimTopIndex, trimBottomIndex, trimLeftIndex, trimRightIndex </inputLong>
    <inputDouble> fillRedLevel, fillGreenLevel, fillBlueLevel </inputDouble>
    <outputLong></outputLong>
    <outputDouble></outputDouble>
    <comment>  </comment>
  </Image1in>

</family>

这是xsl文件:

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

    <html>
      <body>
        <h2>Routines</h2>
        <table border="2">

          <tr bgcolor="#7f7f7f">
            <td style="text-align:left">Title</td>
            <td style="text-align:left">Symbol</td>
            <td style="text-align:left">Operation</td>
            <td style="text-align:left">Input Layer 1</td>
          </tr>

          <xsl:for-each select="family/Image1In">
            <xsl:sort select="routine"/>

            <tr>
              <td><xsl:value-of select="routine"/></td>
              <td><xsl:value-of select="symbolic"/></td>
              <td><xsl:value-of select="operation"/></td>
              <td><xsl:value-of select="inputLayer1"/></td>
            </tr>

          </xsl:for-each>

        </table>
      </body>
    </html>

  </xsl:template>
</xsl:stylesheet>

我非常感谢别人可以分享的任何帮助。花了整个周末时间尝试修复。 问候, RONC

2 个答案:

答案 0 :(得分:0)

浏览器不是测试XSLT的好环境。碰巧的是,您的XSLT使用以下错误:

<xsl:for-each select="family/Image1In">

代替:

<xsl:for-each select="family/Image1in">

XML区分大小写,您的表达式不选择任何内容。

但是,您仍然应该看到表格的标题行。相反,您看到的是XML的原始文本,表明您的浏览器正在应用“相同来源”安全规则,并阻止了对样式表的请求。

答案 1 :(得分:-1)

您可以尝试使用此代码<xsl:for-each select="family/Image1in">插入的<xsl:for-each select="family/Image1In"> 或者,如果要同时删除前导空格和尾随空格,请使用{{1 }}。

normalize-space()

演示:https://xsltfiddle.liberty-development.net/3NSSEv5