XSL |通过查找文档排序

时间:2012-01-10 16:41:36

标签: xml xslt sorting document

gulp 第一篇文章......

情况

我使用两个XML文件进行XSL转换。 File1处理存储,而File2存储Layout-Information。

数据

<item id="1100326">
   <node1> ... </node1>
   [...]
</item>

布局

 <topnews>
    <item vieworder="1">1100326</item>
    <item vieworder="2">1100724</item>
 </topnews>


我设法“提取”layout-XML中列出的节点:

<xsl:for-each select="item[@id=document($document)//topnews/item]" />


问题

我很难通过layoutXML中的 vieworder 属性对数据进行排序。我很感激任何帮助,我愿意向大师们学习! :)

2 个答案:

答案 0 :(得分:0)

+1代表一个好的第一个问题。我就是这样做的....

<强> data.xml中

<doc>
  <item id="1100724">
    <node1>Should be second.</node1>
  </item>
  <item id="1100326">
    <node1>Should be first.</node1>
  </item>
</doc>

<强> layout.xml

<topnews>
  <item vieworder="1">1100326</item>
  <item vieworder="2">1100724</item>
</topnews>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="/doc">
    <doc>
      <xsl:apply-templates select="item">
        <xsl:sort select="document('layout.xml')/topnews/item[.=current()/@id]/@vieworder" data-type="number"/>
      </xsl:apply-templates>      
    </doc>
  </xsl:template>

</xsl:stylesheet>

<强>的Output.xml

<doc>
   <item id="1100326">
      <node1>Should be first.</node1>
   </item>
   <item id="1100724">
      <node1>Should be second.</node1>
   </item>
</doc>

答案 1 :(得分:0)

您可以使用XSLT 2.0(由Saxon 9或AltovaXML支持)吗?

你可以这样做,例如。

<xsl:variable name="layout-doc" select="document('layout.xml')"/>

<xsl:key name="k1" match="topnews/item" use="."/>

<xsl:template match="/">
  <xsl:for-each select="//item[key('k1', @id, $layout-doc)]">
    <xsl:sort select="xs:integer(key('k1', @id, $layout-doc)/@vieworder)"/>
  </xsl:for-each>
</xsl:template>
相关问题