Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带param check的XSLT if子句进行排序_Javascript_Xml_Xslt - Fatal编程技术网

Javascript 带param check的XSLT if子句进行排序

Javascript 带param check的XSLT if子句进行排序,javascript,xml,xslt,Javascript,Xml,Xslt,为什么在下面的代码中添加if子句时会出现错误 如果删除XSLT中的If子句,所有数据都可以按预期显示 XSLT: 编辑~ 我使用记事本和chrome开发了一个javascript网站,在设置orderTag1或OrderTag2时对数据进行排序 编辑~ 修改为将if子句放置在每个的外部。问题解决了 <xsl:if test="$orderTag1 != '' and $orderTag2 = ''"> <xsl:for-each select="*/item">

为什么在下面的代码中添加if子句时会出现错误

如果删除XSLT中的If子句,所有数据都可以按预期显示

XSLT:

编辑~

我使用记事本和chrome开发了一个javascript网站,在设置orderTag1或OrderTag2时对数据进行排序

编辑~

修改为将if子句放置在每个的外部。问题解决了

<xsl:if test="$orderTag1 != '' and $orderTag2 = ''">
    <xsl:for-each select="*/item">
        <xsl:sort select="*[name()=$orderTag1]" order="{$orderType1}"/>
            <tr>
                <xsl:apply-templates/>
            </tr>
    </xsl:for-each>
</xsl:if>

您还没有说您实际遇到了什么错误,但这应该是一个类似于
的错误。“xsl:sort”不能是“xsl:if”元素的子元素。
。这正是它所说的
xsl:sort
应该是
xsl:for each
xsl:apply templates
的子项,而不是
xsl:if

您也没有说您试图用XSLT实现什么,但看起来您希望排序是可配置的,基于参数。如果是这样,你可以这样做

<xsl:for-each select="*/item">
    <xsl:sort select="*[name()=$orderTag1]" order="{$orderType1}"/>
    <xsl:sort select="*[name()=$orderTag2]" order="{$orderType2}"/>
    <tr>
        <xsl:apply-templates/>
    </tr>
</xsl:for-each>
例如,试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name = "orderTag1"/>
<xsl:param name = "orderTag2"/>
<xsl:param name = "orderType1" select="'ascending'" />
<xsl:param name = "orderType2" select="'ascending'" />

<xsl:template match="/">
  <table>
    <xsl:for-each select="*/item">
        <xsl:sort select="*[name()=$orderTag1]" order="{$orderType1}"/>
        <xsl:sort select="*[name()=$orderTag2]" order="{$orderType2}"/>
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:for-each>
  </table>
  </xsl:template>

  <xsl:template match="*[not(*)]">
    <td>
        <xsl:value-of select="."/>
    </td>
  </xsl:template>
</xsl:stylesheet>

或者,如果您希望确保订单类型始终设置正确,您可以更加健壮,如下所示:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name = "orderTag1"/>
<xsl:param name = "orderTag2"/>
<xsl:param name = "orderType1" />
<xsl:param name = "orderType2" />

<xsl:template match="/">
  <table>
    <xsl:variable name="ensureOrderType1">
        <xsl:choose>
            <xsl:when test="$orderType1 != 'ascending' and $orderType1 != 'descending'">ascending</xsl:when>
            <xsl:otherwise><xsl:value-of select="$orderType1" /></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="ensureOrderType2">
        <xsl:choose>
            <xsl:when test="$orderType2 != 'ascending' and $orderType2 != 'descending'">ascending</xsl:when>
            <xsl:otherwise><xsl:value-of select="$orderType2" /></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>


    <xsl:for-each select="*/item">
        <xsl:sort select="*[name()=$orderTag1]" order="{$ensureOrderType1}"/>
        <xsl:sort select="*[name()=$orderTag2]" order="{$ensureOrderType2}"/>
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:for-each>
  </table>
  </xsl:template>

  <xsl:template match="*[not(*)]">
    <td>
        <xsl:value-of select="."/>
    </td>
  </xsl:template>
</xsl:stylesheet>

提升
提升

很抱歉我的描述不好。我使用记事本和chrome开发了一个javascript网站,在设置orderTag1或OrderTag2时对数据进行排序。Chrome在控制台中没有显示任何错误,只返回一个空页面,所以我不知道错误到底是什么。W3School只是声明元素应该在内部。非常感谢你的回答!如何知道“xsl:sort”不能是“xsl:if”元素的子元素?请尝试在测试XSLT。例如:非常感谢!找到一个没有错误的bug是非常令人沮丧的。这真的很有帮助!!!
<xsl:param name = "orderType1" select="'ascending'" />
<xsl:param name = "orderType2" select="'ascending'" />
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name = "orderTag1"/>
<xsl:param name = "orderTag2"/>
<xsl:param name = "orderType1" select="'ascending'" />
<xsl:param name = "orderType2" select="'ascending'" />

<xsl:template match="/">
  <table>
    <xsl:for-each select="*/item">
        <xsl:sort select="*[name()=$orderTag1]" order="{$orderType1}"/>
        <xsl:sort select="*[name()=$orderTag2]" order="{$orderType2}"/>
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:for-each>
  </table>
  </xsl:template>

  <xsl:template match="*[not(*)]">
    <td>
        <xsl:value-of select="."/>
    </td>
  </xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name = "orderTag1"/>
<xsl:param name = "orderTag2"/>
<xsl:param name = "orderType1" />
<xsl:param name = "orderType2" />

<xsl:template match="/">
  <table>
    <xsl:variable name="ensureOrderType1">
        <xsl:choose>
            <xsl:when test="$orderType1 != 'ascending' and $orderType1 != 'descending'">ascending</xsl:when>
            <xsl:otherwise><xsl:value-of select="$orderType1" /></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="ensureOrderType2">
        <xsl:choose>
            <xsl:when test="$orderType2 != 'ascending' and $orderType2 != 'descending'">ascending</xsl:when>
            <xsl:otherwise><xsl:value-of select="$orderType2" /></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>


    <xsl:for-each select="*/item">
        <xsl:sort select="*[name()=$orderTag1]" order="{$ensureOrderType1}"/>
        <xsl:sort select="*[name()=$orderTag2]" order="{$ensureOrderType2}"/>
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:for-each>
  </table>
  </xsl:template>

  <xsl:template match="*[not(*)]">
    <td>
        <xsl:value-of select="."/>
    </td>
  </xsl:template>
</xsl:stylesheet>