2

我正在使用 xsl 将我的 xml 数据转换为 html 文档,但是当我进行调试时,我最终在屏幕上没有我的 xml 信息,只有 html 标记。这是我的 sample.xml 文件我的 xsl 文件和我得到的输出。

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:tns="http://www.example.org/recipe"
>
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
      <html>
        <body>
          <!-- Recipe Name-->
          <h2>
            <xsl:value-of select="tns:recipe_name/tns:name"></xsl:value-of>
          </h2>
          <!-- Line Break-->
          <br>
          </br>
          <!--Recipe Picture-->

            <xsl:value-of select="tns:recipe_name/tns:icon"></xsl:value-of>


          <!--Portion Size-->
          <br></br>
          <h3>Portion Size</h3>

              <xsl:value-of select="tns:portion_size/tns:portion_num"></xsl:value-of>

              <xsl:value-of select="tns:add_time"></xsl:value-of>

              <xsl:value-of select="tns:ingredient/rnd_amount"></xsl:value-of>

          <!--Prep Time + Add Time-->
          <br></br>
          <h3>Prep Time</h3>
          <xsl:value-of select="tns:preparation_time"></xsl:value-of>

          <!--Description-->
          <br></br>
          <h3> Description </h3>
          <xsl:value-of select="tns:description"></xsl:value-of>

          <!--Utensil-->
          <!-- Icon-->
          <br></br>
          <h3> Utensil </h3>

            <xsl:value-of select="tns:utensil/tns:icon"></xsl:value-of>


          <xsl:value-of select="tns:utensil/tns:utensil_description"></xsl:value-of>

          <!-- Ingredients-->
          <br></br>
          <h3>Ingredients</h3>

          <table border="1">
            <tr>
              <th>quantity amount</th>
              <th>quantity name</th>
              <th>ingredient_name</th>
            </tr>


            <xsl:for-each select="tns:ingredient">
              <tr>
                <td>
                  <xsl:value-of select="tns:quantity_amount" />
                </td>
                <td>
                  <xsl:value-of select="tns:quantity_name" />
                </td>
                <td>
                  <xsl:value-of select="tns:ingredient_name" />
                </td>
              </tr>
            </xsl:for-each>
          </table>

          <br></br>

            <xsl:value-of select="tns:ingredient/tns:icon"></xsl:value-of>


          <!-- Method + Steps-->
          <br></br>

          <xsl:value-of select="tns:step_number"></xsl:value-of>

          <xsl:value-of select="tns:step_details"></xsl:value-of>

          </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

XML

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio Developer Edition 9.0.11.3078 (http://www.liquid-technologies.com) -->
<tns:recipe xmlns:tns="http://www.example.org/recipe" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/recipe C:\Users\Nick\Documents\FYP\recipeProject\SampleFile.xsd">
    <tns:recipe_name>
        <tns:name>string</tns:name>
        <tns:icon>string</tns:icon>
    </tns:recipe_name>
    <tns:portion_size>
        <tns:portion_num>-2014</tns:portion_num>
    </tns:portion_size>
    <tns:preparation_time>-8042</tns:preparation_time>
    <tns:add_time>7072</tns:add_time>
    <tns:description>string</tns:description>
    <tns:utensil>
        <tns:number>string</tns:number>
        <tns:utensil_description>string</tns:utensil_description>
        <tns:icon>string</tns:icon>
    </tns:utensil>
    <tns:utensil />
    <tns:ingredient>
        <tns:quantity_amount>9772.474905803945093</tns:quantity_amount>
        <tns:ingredient_name>string</tns:ingredient_name>
        <tns:substitute>string</tns:substitute>
        <tns:rnd_amount>5870</tns:rnd_amount>
    </tns:ingredient>
    <tns:ingredient>
        <tns:quantity_amount>-2413.005094196054907</tns:quantity_amount>
        <tns:ingredient_name>string</tns:ingredient_name>
        <tns:icon>string</tns:icon>
        <tns:rnd_amount>4650</tns:rnd_amount>
    </tns:ingredient>
    <tns:ingredient>
        <tns:quantity_amount>2620.874905803945093</tns:quantity_amount>
        <tns:quantity_name>string</tns:quantity_name>
        <tns:ingredient_name>string</tns:ingredient_name>
        <tns:icon>string</tns:icon>
        <tns:substitute>string</tns:substitute>
    </tns:ingredient>
    <tns:ingredient>
        <tns:quantity_amount>2054.134905803945093</tns:quantity_amount>
        <tns:ingredient_name>string</tns:ingredient_name>
        <tns:substitute>string</tns:substitute>
    </tns:ingredient>
    <tns:ingredient>
        <tns:quantity_amount>-238.245094196054907</tns:quantity_amount>
        <tns:quantity_name>string</tns:quantity_name>
        <tns:ingredient_name>string</tns:ingredient_name>
        <tns:icon>string</tns:icon>
        <tns:rnd_amount>588</tns:rnd_amount>
    </tns:ingredient>
    <tns:method>
        <tns:step>
            <tns:step_number>8011</tns:step_number>
            <tns:step_details>string</tns:step_details>
        </tns:step>
    </tns:method>
</tns:recipe>

我得到的输出

<html xmlns:tns="http://www.example.org/recipe">
  <body>
    <h2>
    </h2><br><br><h3>Portion Size</h3><br><h3>Prep Time</h3><br><h3> Description </h3><br><h3> Utensil </h3><br><h3>Ingredients</h3>
    <table border="1">
      <tr>
        <th>quantity amount</th>
        <th>quantity name</th>
        <th>ingredient_name</th>
      </tr>
    </table><br><br></body>
</html>
4

1 回答 1

3

您在匹配中缺少一个级别。在您的 XSLT 中,更改此行...

<xsl:template match="/">

<xsl:template match="/">
    <xsl:apply-templates select="tns:recipe" />
</xsl:template>

<xsl:template match="tns:recipe">
于 2013-11-13T22:57:11.647 回答