2017-10-12 38 views
0

我有这个任务的主要问题,我无法弄清楚如何进行排序。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/numbers/number"> 
          <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"?> 

     <numbers> 
      <number>A</number> 
      <number>C</number> 
      <number>B</number> 
      <number>E</number> 
      <number>D</number> 
     </numbers> 

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

我现在得到的是一个表:

1 - 狗

2 - 马

3 - 牛

4 - 蛇

5 - 蜘蛛

我想要得到的是:

1 - 狗

2 - 牛

3 - 马

4 - 蜘蛛

5 - 蛇

我可以不知道如何在for-each循环之后对列进行排序,只有column_1。 试图在这里找到类似的问题,但无济于事。 Ps。对于格式化抱歉,不确定缩进是否正确。

回答

0

我认为你不想要<xsl:sort select="." order="ascending"/>并将其删除,然后你可以使用<td class="_fileA"><xsl:value-of select="position()" /></td>作为第一列,第二列使用<td class="_fileB"><xsl:value-of select="$fileB//animal[position() = current()]" /></td>

为一体的完整样式表,我得到

<?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="text/html" 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/numbers/number"> 

         <tr>         
          <td class="_fileA"><xsl:value-of select="position()" /></td> 
          <td class="_fileB"><xsl:value-of select="$fileB//animal[position() = current()]" /></td> 
         </tr> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

而且给我的输出

column_1 column_2 
1 dog 
2 cow 
3 horse 
4 spider 
5 snake 

在线https://martin-honnen.github.io/xslt/2017/test201710120301.xml

+0

使用position()会在第一列给我数字按升序排列。第二列保持不变。 –

+0

@goat_pee,对不起,是的,第二列也需要更改,请参阅编辑,我弄糊涂了变量'current_node'的名称。 –

+0

Thnx用于输入。出于某种原因,这不会排序第一列和第二列是空的......我不能想象没有排序(),但是再次,我只是一个菜鸟,当涉及到XSLT。 –

相关问题