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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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
根据XML源中的顺序应用XSLT模板_Xml_Xslt - Fatal编程技术网

根据XML源中的顺序应用XSLT模板

根据XML源中的顺序应用XSLT模板,xml,xslt,Xml,Xslt,嗨,我有一个xml文件,我必须转换成html页面。此文件的结构(简化)如下: 我的XSL文件看起来像(简化的): 转换是有效的,但是现在我必须以这样一种方式强制输出,即尊重元素的顺序。据我所知,使用模板迫使XSL遍历为其定义的所有元素。因此,选项模板中的第一个将输出所有内容元素,然后将输出所有选项元素(位于子级别上)。问题是,我必须保持与XML文档中相同的顺序。因此,截面A的输出应为: 内容1 内容2 选择3 内容4 而不是我的解决方案的输出: 内容1 内容2 内容4 选择3

嗨,我有一个xml文件,我必须转换成html页面。此文件的结构(简化)如下:


我的XSL文件看起来像(简化的):


转换是有效的,但是现在我必须以这样一种方式强制输出,即尊重元素的顺序。据我所知,使用模板迫使XSL遍历为其定义的所有元素。因此,选项模板中的第一个
将输出所有内容元素,然后将输出所有选项元素(位于子级别上)。问题是,我必须保持与XML文档中相同的顺序。因此,截面A的输出应为:

  • 内容1
  • 内容2
  • 选择3
  • 内容4
而不是我的解决方案的输出:

  • 内容1
  • 内容2
  • 内容4
  • 选择3
因此,它将首先处理所有内容元素,而不是选项元素。
如何强制xml中的输出顺序?

您的测试是不必要的:如果节点不存在,则不会执行与之匹配的模板。而不是:

<xsl:template match="option">       
    <!-- Output html -->
    <xsl:if test="content">             
        <xsl:apply-templates select="content"/>
    </xsl:if>   
    <xsl:if test="option">  
        <xsl:apply-templates select="option"/>
    </xsl:if>                       
</xsl:template>

您可以简单地执行以下操作:

<xsl:template match="option">       
    <!-- Output html -->
    <xsl:apply-templates select="content | option"/>        
</xsl:template>

您的测试是不必要的:如果节点不存在,那么将不会执行与之匹配的模板。而不是:

<xsl:template match="option">       
    <!-- Output html -->
    <xsl:if test="content">             
        <xsl:apply-templates select="content"/>
    </xsl:if>   
    <xsl:if test="option">  
        <xsl:apply-templates select="option"/>
    </xsl:if>                       
</xsl:template>

您可以简单地执行以下操作:

<xsl:template match="option">       
    <!-- Output html -->
    <xsl:apply-templates select="content | option"/>        
</xsl:template>

默认情况下,XSLT处理器按文档顺序处理输入XML

你所需要做的就是使用
并让开

为了说明我的意思,下面的简单转换将
映射到
映射到
映射到

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

    <xsl:template match="/">
        <html>
            <xsl:apply-templates select="top/element" />
        </html>
    </xsl:template>

    <xsl:template match="element">
        <section>
            <xsl:apply-templates />
        </section>
    </xsl:template>

    <xsl:template match="option">
        <div>
            <span><xsl:value-of select="@id" /></span>
            <xsl:apply-templates />
        </div>
    </xsl:template>

    <xsl:template match="content">
        <p>
            <span><xsl:value-of select="@id" /></span>
            <xsl:apply-templates />
        </p>
    </xsl:template>
</xsl:stylesheet>
由于您希望所有子项都按原始顺序处理,因此最简单的方法是:

<xsl:apply-templates />

或者,如果您想更具体一些,可以使用联合:

<xsl:apply-templates select="content|option"/>


这也将保持输入顺序。

默认情况下,XSLT处理器以文档顺序处理输入XML

你所需要做的就是使用
并让开

为了说明我的意思,下面的简单转换将
映射到
映射到
映射到

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

    <xsl:template match="/">
        <html>
            <xsl:apply-templates select="top/element" />
        </html>
    </xsl:template>

    <xsl:template match="element">
        <section>
            <xsl:apply-templates />
        </section>
    </xsl:template>

    <xsl:template match="option">
        <div>
            <span><xsl:value-of select="@id" /></span>
            <xsl:apply-templates />
        </div>
    </xsl:template>

    <xsl:template match="content">
        <p>
            <span><xsl:value-of select="@id" /></span>
            <xsl:apply-templates />
        </p>
    </xsl:template>
</xsl:stylesheet>
由于您希望所有子项都按原始顺序处理,因此最简单的方法是:

<xsl:apply-templates />

或者,如果您想更具体一些,可以使用联合:

<xsl:apply-templates select="content|option"/>


这也将维持输入顺序。

您是否可以将模板
match=“option”
修改为:
。没有如果!没有元素的显式调用。相反,让空的
来完成工作(文档顺序保持不变)。您可以将模板
match=“option”
修改为:
。没有如果!没有元素的显式调用。相反,让空的
完成工作(文档顺序保持不变)。