XSLT 变量不起作用

发布于 2024-11-04 18:41:12 字数 1543 浏览 0 评论 0 原文

我有以下一段 XSLT 代码:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">

<xsl:variable name="condition">(coasts='Adriatic Sea')or(coasts='Mediterranean Sea')</xsl:variable>
<xsl:template match="cia">
    <html>
     <head></head>
     <body>
      <table border="1">
       <tr>
        <th>Country</th>
        <th>Capital</th>
        <th>Area</th>
        <th>Population</th>
        <th>Inflation rate</th>
       </tr> 
       <xsl:for-each select="country">
        <xsl:if test="{$condition}">
        <tr>
         <td>
          <xsl:value-of select="@name"/>
         </td>
         <td>
          <xsl:value-of select="@capital"/>
         </td> 
         <td>
          <xsl:value-of select="@total_area"/>
         </td> 
         <td>
          <xsl:value-of select="@population"/>
         </td> 
         <td>
          <xsl:value-of select="@inflation"/>
         </td>
        </tr>
        </xsl:if>
       </xsl:for-each>
      </table>
     </body>
    </html>
  </xsl:template> 
</xsl:stylesheet>

如果将条件表达式直接放入 if 元素中,代码可以正常工作,但是,我的问题是,当我将相同的条件表达式分配给变量,然后在 if 元素中引用它时,它不会不工作。我做错了什么吗?这可能吗? 谢谢!

I have the following piece of XSLT code:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">

<xsl:variable name="condition">(coasts='Adriatic Sea')or(coasts='Mediterranean Sea')</xsl:variable>
<xsl:template match="cia">
    <html>
     <head></head>
     <body>
      <table border="1">
       <tr>
        <th>Country</th>
        <th>Capital</th>
        <th>Area</th>
        <th>Population</th>
        <th>Inflation rate</th>
       </tr> 
       <xsl:for-each select="country">
        <xsl:if test="{$condition}">
        <tr>
         <td>
          <xsl:value-of select="@name"/>
         </td>
         <td>
          <xsl:value-of select="@capital"/>
         </td> 
         <td>
          <xsl:value-of select="@total_area"/>
         </td> 
         <td>
          <xsl:value-of select="@population"/>
         </td> 
         <td>
          <xsl:value-of select="@inflation"/>
         </td>
        </tr>
        </xsl:if>
       </xsl:for-each>
      </table>
     </body>
    </html>
  </xsl:template> 
</xsl:stylesheet>

If put the conditional expression directly in the if element the code works fine, however, my problem is that when I assign the same conditional expression to the variable and then reference it in the if element it doesn't work. Am I doing something wrong? Is this possible?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪少女 2024-11-11 18:41:12

存在多个问题:

  • 中的括号是不必要的;使用
  • 使用以下 xsl:variable 构造:

    ;
    
  • 当您的 xsl:if 中有条件时,将相对于每个 执行测试国家。顶级变量的情况并非如此。变量的值是表达式的结果,而不是表达式本身。如果您坚持使用变量,请在循环内初始化它。

请参阅以下样式表:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/1999/xhtml">
    <xsl:template match="cia">
        <html>
            <head></head>
            <body>
                <table border="1">
                    <tr>
                        <th>Country</th>
                        <th>Capital</th>
                        <th>Area</th>
                        <th>Population</th>
                        <th>Inflation rate</th>
                    </tr>
                    <xsl:for-each select="country">
                        <xsl:variable name="condition"
                            select="(coasts='Adriatic Sea') or 
                                    (coasts='Mediterranean Sea')" />
                        <xsl:if test="$condition">
                            <tr>
                                <td>
                                    <xsl:value-of select="@name" />
                                </td>
                                <td>
                                    <xsl:value-of select="@capital" />
                                </td>
                                <td>
                                    <xsl:value-of select="@total_area" />
                                </td>
                                <td>
                                    <xsl:value-of select="@population" />
                                </td>
                                <td>
                                    <xsl:value-of select="@inflation" />
                                </td>
                            </tr>
                        </xsl:if>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

输入:

<cia>
    <country name="test1" inflation="22">
        <coasts>Adriatic Sea</coasts>
    </country>
    <country name="test2" inflation="7">
        <coasts>No match</coasts>
    </country>
</cia>

输出(仅第一个国家/地区 通过测试):

<html xmlns="http://www.w3.org/1999/xhtml">
    <head></head>
    <body>
        <table border="1">
            <tr>
                <th>Country</th>
                <th>Capital</th>
                <th>Area</th>
                <th>Population</th>
                <th>Inflation rate</th>
            </tr>
            <tr>
                <td>test1</td>
                <td></td>
                <td></td>
                <td></td>
                <td>22</td>
            </tr>
        </table>
    </body>
</html>

请注意,您实际上甚至不需要单独的条件;只需添加一个即可。最好首先只选择所需的元素。该循环产生相同的输出:

<xsl:for-each
    select="country[(coasts='Adriatic Sea') or
                     coasts='Mediterranean Sea')]">
    <tr>
        <td>
            <xsl:value-of select="@name" />
        </td>
        <td>
            <xsl:value-of select="@capital" />
        </td>
        <td>
            <xsl:value-of select="@total_area" />
        </td>
        <td>
            <xsl:value-of select="@population" />
        </td>
        <td>
            <xsl:value-of select="@inflation" />
        </td>
    </tr>
</xsl:for-each>

There are multiple problems:

  • The brackets in <xsl:if test="{$condition}"> are unnecessary; use <xsl:if test="$condition">
  • Use the following xsl:variable construct:

    <xsl:variable name="condition" 
            select="(coasts='Adriatic Sea')or(coasts='Mediterranean Sea')"/>
    
  • When you had the condition in your xsl:if the test was performed relative to each country. This is not the case in a top-level variable. The value of the variable is the result of the expression, not the expression itself. If you insist on a variable, then initialize it inside the loop.

See the following stylesheet:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/1999/xhtml">
    <xsl:template match="cia">
        <html>
            <head></head>
            <body>
                <table border="1">
                    <tr>
                        <th>Country</th>
                        <th>Capital</th>
                        <th>Area</th>
                        <th>Population</th>
                        <th>Inflation rate</th>
                    </tr>
                    <xsl:for-each select="country">
                        <xsl:variable name="condition"
                            select="(coasts='Adriatic Sea') or 
                                    (coasts='Mediterranean Sea')" />
                        <xsl:if test="$condition">
                            <tr>
                                <td>
                                    <xsl:value-of select="@name" />
                                </td>
                                <td>
                                    <xsl:value-of select="@capital" />
                                </td>
                                <td>
                                    <xsl:value-of select="@total_area" />
                                </td>
                                <td>
                                    <xsl:value-of select="@population" />
                                </td>
                                <td>
                                    <xsl:value-of select="@inflation" />
                                </td>
                            </tr>
                        </xsl:if>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Input:

<cia>
    <country name="test1" inflation="22">
        <coasts>Adriatic Sea</coasts>
    </country>
    <country name="test2" inflation="7">
        <coasts>No match</coasts>
    </country>
</cia>

Output (only the first country passes the test):

<html xmlns="http://www.w3.org/1999/xhtml">
    <head></head>
    <body>
        <table border="1">
            <tr>
                <th>Country</th>
                <th>Capital</th>
                <th>Area</th>
                <th>Population</th>
                <th>Inflation rate</th>
            </tr>
            <tr>
                <td>test1</td>
                <td></td>
                <td></td>
                <td></td>
                <td>22</td>
            </tr>
        </table>
    </body>
</html>

Note that you don't really even need a separate condition; it's better to select just the desired elements in the first place. This loop produces the same output:

<xsl:for-each
    select="country[(coasts='Adriatic Sea') or
                     coasts='Mediterranean Sea')]">
    <tr>
        <td>
            <xsl:value-of select="@name" />
        </td>
        <td>
            <xsl:value-of select="@capital" />
        </td>
        <td>
            <xsl:value-of select="@total_area" />
        </td>
        <td>
            <xsl:value-of select="@population" />
        </td>
        <td>
            <xsl:value-of select="@inflation" />
        </td>
    </tr>
</xsl:for-each>
你列表最软的妹 2024-11-11 18:41:12

替换

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

               <xsl:apply templates select="country"/>

另外,添加这些模板

 <xsl:template match="country"/>

 <xsl:template match=
  "country[coasts='Adriatic Sea' 
         or                                        
           coasts='Mediterranean Sea'
         ]">
<xsl:template match=
  "country[coasts='Adriatic Sea'
         or
           coasts='Mediterranean Sea'
         ]">
    <tr>
        <td>
            <xsl:value-of select="@name"/>
        </td>
        <td>
            <xsl:value-of select="@capital"/>
        </td>
        <td>
            <xsl:value-of select="@total_area"/>
        </td>
        <td>
            <xsl:value-of select="@population"/>
        </td>
        <td>
            <xsl:value-of select="@inflation"/>
        </td>
    </tr>
</xsl:template>

您是否注意到“神奇地“消失了?

当然,该代码还可以进一步改进,但是您既没有提供要应用转换的 XML 文档的实例,也没有提供所需的输出。

Replace:

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

with

               <xsl:apply templates select="country"/>

also, add these templates:

 <xsl:template match="country"/>

 <xsl:template match=
  "country[coasts='Adriatic Sea' 
         or                                        
           coasts='Mediterranean Sea'
         ]">
<xsl:template match=
  "country[coasts='Adriatic Sea'
         or
           coasts='Mediterranean Sea'
         ]">
    <tr>
        <td>
            <xsl:value-of select="@name"/>
        </td>
        <td>
            <xsl:value-of select="@capital"/>
        </td>
        <td>
            <xsl:value-of select="@total_area"/>
        </td>
        <td>
            <xsl:value-of select="@population"/>
        </td>
        <td>
            <xsl:value-of select="@inflation"/>
        </td>
    </tr>
</xsl:template>

Did you note that the <xsl:if> "magically" disappeared?

Of course, this code can be improved even further, but you haven't provided neither an instance of the XML document on which the transformation is to be applied, nor the desired output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文