2017-10-13 34 views
1

我有這個任務的主要問題,我無法弄清楚如何進行排序。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()可以被用作索引,但它是一個很好的選擇。我相信這有一個更簡單的解決方案。

回答

2

變化

<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

你的先生,是一個真正的救星!多謝! –

相關問題