【问题标题】:XSLT Select multiple attribute with the same nameXSLT 选择多个同名属性
【发布时间】:2021-03-26 07:03:56
【问题描述】:

早上好,我是 XLS 的新手,我正在尝试显示一个包含“菜单”项的 XML。但是每道菜都包含许多成分标签,我无法将它们显示在我的表中的列表中。 这是我的 XML:

<Menu>
    <dish Id='001'>
        <general_info>
            <name>Margharita</name>
            <description>Mozzarela slices, tomatoes, basil and extra olive oil</description>
            <type>Pizza</type>
        </general_info>

        <nutrition_info>
            <ingredient>Mozzarela</ingredient>
            <ingredient>tomatoes</ingredient>
            <ingredient>basil</ingredient>
            <ingredient>olive oil</ingredient>
            <ingredient>flour</ingredient>
            <dietary_restrictions>Allergic to Gluten</dietary_restrictions>
        </nutrition_info>

        <price>RS300</price>
    </dish>

    <dish Id='002'>
        <general_info>
            <name>Chicken Burger</name>
            <description>Chicken regular Hamburger</description>
            <type>Burger</type>
        </general_info>

        <nutrition_info>
            <ingredient>bread</ingredient>
            <ingredient>chicken</ingredient>
            <ingredient>tomato</ingredient>
            <ingredient>cheddar</ingredient>
            <ingredient>onions</ingredient>
            <ingredient>lettuce</ingredient>
            <dietary_restrictions></dietary_restrictions>
        </nutrition_info>

        <price>RS290</price>
    </dish>
</Menu>

这是我的 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><center>Menu</center></h2>
          <table border="1">
              <tr bgcolor="orange">
                  <th>Name</th>
                  <th>Description</th>
                  <th>Type</th>
                  <th>Ingredients</th>
                  <th>dietary restrictions</th>
                  <th>Price</th>
              </tr>

              <xsl:for-each select="Menu/dish">
              <tr>
                  <td><xsl:value-of select="general_info/name"/></td>
                  <td><xsl:value-of select="general_info/description"/></td>
                  <td><xsl:value-of select="general_info/type"/></td>
                  <td>
                    <ul>
                      <xsl:for-each select="Menu/dish/nutrition_info">
                      <li><xsl:value-of select="ingredient"/></li>
                      </xsl:for-each>
                    </ul>
                  </td>
                  <td><xsl:value-of select="nutrition_info/dietary_restrictions"/></td>
                  <td><xsl:value-of select="price"/></td>
              </tr>
              </xsl:for-each>
          </table>
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>

我正在努力实现这样的目标:

Name      | Description | TYPE | Ingredients | dietary restrictions | Price
Margharita|.............|Pizza |list of items|......................|......
etc

请注意,每道菜可能有不同数量的成分属性。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    你需要使用相对路径

    <ul>
      <xsl:for-each select="nutrition_info">
        <xsl:for-each select="ingredient">
          <li><xsl:value-of select="."/></li>
        </xsl:for-each>
      </xsl:for-each>
    </ul>
    

    【讨论】:

    • 为什么不使用模板而不是 for-each?
    • 恕我直言,非 xslt 专家更容易理解...使用模板也可以
    • 是的,我非常同意你的观点!作为初学者,我也遇到了问题。
    • 非常感谢!它运作良好。但是你可以在同一行打印列表吗?举例用逗号分隔?
    • 使用字符串连接:&lt;xsl:for-each select="nutrition_info"&gt; &lt;xsl:value-of select="string-join( (ingredient), ',')" /&gt; &lt;/xsl:for-each&gt;
    【解决方案2】:

    应用模板的使用示例:

    <?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>
              <center>Menu</center>
            </h2>
            <table border="1">
              <thead>
                <tr>
                  <th>Name</th>
                  <th>Description</th>
                  <th>Type</th>
                  <th>Ingredients</th>
                  <th>dietary restrictions</th>
                  <th>Price</th>
                </tr>
              </thead>
              <tbody>
                <xsl:apply-templates select="Menu/dish"/>
              </tbody>
            </table>
          </body>
        </html>
      </xsl:template>
      
      <xsl:template match="dish">
        <tr>
          <td>
            <xsl:value-of select="general_info/name"/>
          </td>
          <td>
            <xsl:value-of select="general_info/description"/>
          </td>
          <td>
            <xsl:value-of select="general_info/type"/>
          </td>
          <td>
            <xsl:apply-templates select="nutrition_info"/>
          </td>
          <td>
            <xsl:value-of select="nutrition_info/dietary_restrictions"/>
          </td>
          <td>
            <xsl:value-of select="price"/>
          </td>
        </tr>
        
      </xsl:template>
    
      <xsl:template match="nutrition_info">
        <ul>
          <xsl:apply-templates select="ingredient"/>
        </ul>
      </xsl:template>
      
      <xsl:template match="ingredient">
        <li>
          <xsl:value-of select="."/>
        </li>
      </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多