简体   繁体   English

XSL | 通过查找文档排序

[英]XSL | Sorting via Lookup Document

gulp First post.... gulp第一篇文章...

The Situation 情况

I use two XML Files for my XSL Transformation. 我将两个XML文件用于XSL转换。 File1 handles Storage while File2 stores Layout-Information. File1处理存储,而File2存储布局信息。

Data 数据

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

Layout 布局

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


I managed to "extract" nodes listed in the layout-XML solely: 我设法仅“提取” layout-XML中列出的节点:

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


The Problem 问题

I am having serious trouble sorting the data via the vieworder attribute from the layoutXML. 我在通过layoutXML的vieworder属性对数据进行排序时遇到了严重的麻烦。 I'd appreciate any help and am willing to learn from the masters! 我将不胜感激,并愿意向大师学习! :) :)

+1 for a good first question. +1是第一个好问题。 This is how I would do it.... 这就是我要做的。

data.xml data.xml

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

layout.xml layout.xml

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

XSLT 1.0 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 output.xml

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

Can you use XSLT 2.0 (as supported by Saxon 9 or AltovaXML)? 您可以使用XSLT 2.0(受Saxon 9或AltovaXML支持)吗?

With that you could simply do eg 这样,你可以简单地做例如

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM