通过我的XML表的每个循环的XSL - 显示表存根但没有文本

时间:2015-10-20 06:18:25

标签: xml xslt xml-parsing xslt-1.0 xslt-2.0

所以我有一个XML文档和一个XSL样式表。我循环遍历我的一个元素并尝试显示表中的所有元素 - 它显示表的正确数量的“存根”但没有文本。有人可以帮忙吗?

这是我的XML文档:

    <?xml version="1.0" encoding="ISO8859-1" ?>
      <?xml-stylesheet type="text/xsl" href="xrt.xsl"?>
        <Inventory>
          <DatabaseName>
            <GlobalName>Tom</GlobalName>
            <Function>production</Function>
            <Domain>tom.info</Domain>
             <Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
             <Attributes Type="Production" Version="20ix"/>
             <Comments>
             ...
             </Comments>
             <Usage>
             500
             </Usage>
             </DatabaseName>


             <WebserverName>
                <GlobalName>Jim</GlobalName>
                <Function>distribution</Function>
                <Domain>jim1235.com</Domain>
                 <Administrator EmailAlias="rknowles" Extension="134237">Richard Knowles</Administrator>
                 <Administrator EmailAlias="thoffman" Extension="222237">Tom Hoffman</Administrator>
                 <Attributes Type="Production" Version="20ix"/>
                 <Comments>
                 ...
                 </Comments>
                 <Usage>
                 1200
                 </Usage>
               </WebserverName>
             </Inventory>

这是我的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>
         <h1>The Inventory</h1>
         <xsl:apply-templates/>
        </body>
      </html>
      </xsl:template>

      <!--DatabaseName Template-->
      <xsl:template match="Inventory/DatabaseName">
      <h2><xsl:value-of select="GlobalName"/></h2>
      <xsl:choose>
        <xsl:when test="Usage &gt;= 500">
        <p>Its function is <xsl:value-of select="Function"/> and its domain is      <xsl:value-of select="Domain"/>. The database is widely used.</p>
        <h3>Administrators</h3>
        <table border="1">
          <tbody>
            <xsl:for-each select="Administrator">
            <tr>
              <td>
                <xsl:value-of select="Administrator"/>
              </td>
            </tr>
            </xsl:for-each>
          </tbody>
        </table>

        </xsl:when>
        <xsl:otherwise>
          <p>Its function is <xsl:value-of select="Function"/> and its domain is <xsl:value-of select="Domain"/>. The database is not used widely.</p>
          <h3>Administrators</h3>

          <table border="1">
            <tbody>
              <xsl:for-each select="Administrator">
              <tr>
                <td>
                  <xsl:value-of select="Administrator"/>
                </td>
              </tr>
              </xsl:for-each>
            </tbody>
          </table>       
        </xsl:otherwise>
      </xsl:choose>
</xsl:template>

<!--WebserverName Template-->
    <xsl:template match="WebserverName">
      <h2><xsl:value-of select="GlobalName"/></h2>
        <xsl:choose>
        <xsl:when test="Usage &gt;= 1000">
        <p>Its function is <xsl:value-of select="Function"/> and its domain is <xsl:value-of select="Domain"/>. The webserver is widely used.</p>
        <h3>Administrators</h3>
        <table border="1">
          <tbody>
            <xsl:for-each select="Administrator">
              <tr>
                <td>
                  <xsl:value-of select="Administrator"/>
                </td>
              </tr>
              </xsl:for-each>   
          </tbody>
        </table>
        </xsl:when>
        <xsl:otherwise>
          <p>Its function is <xsl:value-of select="Function"/> and its domain is <xsl:value-of select="Domain"/>. The webserver is not used widely.</p>
          <h3>Administrators</h3>
          <xsl:for-each select="Administrator">
          <table border="1">
            <tbody>
              <xsl:for-each select="Administrator">
                <tr>
                  <td>
                    <xsl:value-of select="Administrator"/>
                  </td>
                </tr>
              </xsl:for-each>
            </tbody>
          </table>
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
</xsl:template>

 </xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

<xsl:for-each select="Administrator">

将您置于Administrator的上下文中。从这个背景来看,

<xsl:value-of select="Administrator"/>

不选择任何内容,因为上下文节点没有名为Administrator的子项。你应该使用:

<xsl:value-of select="."/>

您的问题无关,但您不必重复相同的代码两次(或更多次) - 请参阅:https://en.wikipedia.org/wiki/Don%27t_repeat_yourself