Xml 元素的XSLT切换顺序

Xml 元素的XSLT切换顺序,xml,xslt,sitecore,Xml,Xslt,Sitecore,我需要切换元素,例如在xml中: <configuration> <sites> <site name="site1"/> <site name="site2" /> <site name="site3"/> <site name="site4"/> </sites> </configuration> 致: 通过XSLT。有人能帮上忙吗 <xsl:tem

我需要切换元素,例如在xml中:

<configuration>
  <sites>
   <site name="site1"/>
   <site name="site2" />
   <site name="site3"/>
   <site name="site4"/>
  </sites>
</configuration>

致:


通过XSLT。有人能帮上忙吗

<xsl:template match="node()|@*">
  <xsl:copy>
     <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<xsl:variable name="site1" select="configuration/sites/site[@name='site1']" />

<xsl:variable name="site2" select="configuration/sites/site[@name='site2']" />

<xsl:template match="site[@name='site1'"/>
<xsl:template match="site[@name='site2'"/>


<xsl:template match="configuration/sitecore/sites">
  <xsl:apply-templates select="node()|@*"/>
  <xsl:value-of select="$site1"/>
  <xsl:value-of select="$site2"/>
</xsl:template>


我试图做的是,我不确定是否可以使用某个变量将所有特定的元素内容存储到其中,删除相应的元素副本节点的其余部分,并将其附加到末尾

谢谢


Krp0

如果无法通过
xsl:sort
所需的成对排序关系来指定
站点
元素的所需顺序,那么您可能希望仅根据其
@name
属性区分的预定顺序来重新排列一组预定的
站点

XSLT1.0 信用:感谢您的建议。

OP问题,更准确 在游戏后期,OP需要的似乎不是通过名称对四个项目中的两个项目进行过滤,而是提取第一个十个项目,并在所有其他项目之后输出它们(在编写样式表时不知道确切的数字)

解决原则 使用
position()
运算符仅应用模板两次:


或者(可读性较差,但不会混淆一些不完整的解析器和语法高亮):


再生溶液 此文件:


转换为此(缺少一些换行):


由此


测试

xsltproc-o out.xml sheet.xsl in.xml&&cat out.xml

规则是什么?查找
xsl:sort
…我需要site1/site2位于site3/site4的定义之后。必须是输出xml中的最后一个。我有很多这个网站的定义,我通过它们的名字来识别它们。请用XSLT更新你的问题。这里有一个关于starts with(如果您没有)的参考:我还要提到XSLT2.0通过简单地编写
,使其更加紧凑。是的,这是一个很好的建议。答案已更新。谢谢我需要更详细地说明我到底想做什么。我有site1-site10和site11-site100,这10个站点必须在site100之后。但在现实中,我不知道有多少个站点定义是唯一的,我知道我需要删除这10个站点。如果这个答案没有帮助,是的,我同意你必须准确地说明你想做什么。请让我知道我的解释是否正确,或者需要更多地说明我的意图。
<xsl:template match="node()|@*">
  <xsl:copy>
     <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<xsl:variable name="site1" select="configuration/sites/site[@name='site1']" />

<xsl:variable name="site2" select="configuration/sites/site[@name='site2']" />

<xsl:template match="site[@name='site1'"/>
<xsl:template match="site[@name='site2'"/>


<xsl:template match="configuration/sitecore/sites">
  <xsl:apply-templates select="node()|@*"/>
  <xsl:value-of select="$site1"/>
  <xsl:value-of select="$site2"/>
</xsl:template>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="sites">
    <xsl:copy>
      <xsl:apply-templates select="site[@name='site3']"/>
      <xsl:apply-templates select="site[@name='site4']"/>
      <xsl:apply-templates select="site[@name='site1']"/>
      <xsl:apply-templates select="site[@name='site2']"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="sites">
    <xsl:copy>
      <xsl:apply-templates select="site[@name='site3'],
                                   site[@name='site4'],
                                   site[@name='site1'],
                                   site[@name='site2']"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>