2010-10-25 87 views
1
XML元素

假设我有一个像这样订购使用XSLT

XML文件的XML文档:

<document> 
    <educationalsection> 
    educational details 
    </educationalsection> 

    <professionalsection> 
    professional section details 
    </professionalsection> 
</document> 

我创建了XSL将其融合到我需要的格式,但问题是,如果我想改变部分的顺序我该怎么做?例如,如果我想专业部分来教育的顶部而不改变XML,那怎么可能?我需要添加到现有的xsl或xml中,以便当我的Web应用程序发送xml进行转换时,它可以具有Web应用程序指定的不同元素顺序。

回答

2

该样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:param name="pOrder" select="'professionalsection,educationalsection'"/> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"> 
       <xsl:sort select="string-length(
            substring-before(
             concat(',',$pOrder,','), 
             concat(',',name(),',')))"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

输出:

<document> 
    <professionalsection> 
    professional section details 
    </professionalsection> 
    <educationalsection> 
    educational details 
    </educationalsection> 
</document> 
+0

这是一个动态的解决方案?这意味着你可以自动设置部分的顺序? – 2010-10-25 12:45:20

+0

@Sultan Saadat:您应该将新订单字符串作为'pOrder'参数传递给处理器。 – 2010-10-25 13:25:56

+0

谢谢你的帮助:)这真的很有帮助! :) – 2010-10-26 08:51:32

1

xsl:apply-templatesxsl:for-each元素可以具有xsl:sort子元素,可用于排序所选子节点。在需要时使用不同的排序顺序。

您还可以使用xsl:template上的mode属性使用不同的排序顺序选择不同的模板。

+0

为模式的任何代码示例? – 2010-10-25 11:42:05

+0

@Sultan Saadat - 'mode'的例子。这些不是特定于排序,但应该给你的总体思路。 http://zvon.org/xxl/XSLTreference/OutputExamples/example_1_20_frame.html – Oded 2010-10-25 11:47:26

+0

我明白了,但我的部分是不同的,所以我将不得不使用部分序列号字段的类型优先每个部分,然后反映在输出... – 2010-10-25 11:51:03