XSLT使用来自外部文档的信息对表格进行排序

问题描述:

我有这个任务的主要问题,我无法弄清楚如何进行排序。XSLT使用来自外部文档的信息对表格进行排序

我想排序XSLT中的一个表,我正在导入一个.XSL。在这个.XSL中,我有两个引用的外部.XSL。输出应该是html。

mainXSL.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" media-type="image/svg" indent="yes" encoding="UTF-8"/> 

       <xsl:variable name="fileA" select="document(/importFiles/docs/@fileA)" /> 
       <xsl:variable name="fileB" select="document(/importFiles/docs/@fileB)" /> 
    <xsl:template match="/"> 
     <html> 
      <head> 
       <title> 
        Task1 
       </title> 
      </head>   
       <body> 
        <table align="center" border="1"> 
         <tr> 
          <th>column_1</th> 
          <th>column_2</th> 
         </tr> 

         <xsl:for-each select="$fileA/letters/letter"> 
          <xsl:sort select="." order="ascending"/> 
           <xsl:variable name="current_node" select="position()"/> 
            <tr>         
             <td class="_fileA"><xsl:value-of select="." /></td> 
             <td class="_fileB"><xsl:value-of select="$fileB//animal[$current_node]" /></td> 
            </tr> 
          </xsl:for-each> 
        </body> 
       </html> 
</xsl:template> 

INDEX.XML

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="mainXSL.xsl"?> 

<importFiles> 
    <docs fileA = "fileA.xml" /> 
    <docs fileB = "fileB.xml" /> 
</importFiles> 

fileA.xml

<?xml version="1.0" encoding="UTF-8"?> 

     <letters> 
      <letter>A</letter> 
      <letter>C</letter> 
      <letter>B</letter> 
      <letter>E</letter> 
      <letter>D</letter> 
     </letters> 

fileB.xml

<?xml version="1.0" encoding="UTF-8"?> 

     <animals> 
      <animal>dog</animal> 
      <animal>horse</animal> 
      <animal>cow</animal> 
      <animal>snake</animal> 
      <animal>spider</animal> 
     </animals> 

所以在fileA.xml的字母attatched的动物在同一行中fileB.xml

我现在得到的是一个表:

A - 狗

乙 - 马

ç - 牛

d - 蛇

ë - 蜘蛛

我想要得到的是:

A - 狗

乙 - 牛

Ç - 马

d - 蜘蛛

ë - 蛇

我可以不知道如何在for-each循环之后对列进行排序,只有column_1。 试图在这里找到类似的问题,但无济于事。 我之前发布了一个类似的问题,并得到了正确的答案,但我忘记了编辑字母的数字。使用数字使得position()更容易。我假设在这种情况下,position()可以被用作索引,但它是一个很好的选择。我相信这有一个更简单的解决方案。

变化

<xsl:variable name="current_node" select="position()"/> 

<xsl:variable name="orig-pos"><xsl:number/></xsl:variable> 

然后用

<xsl:value-of select="$fileB//animal[position() = $orig-pos]" /> 
+0

你的先生,是一个真正的救星!多谢! –