2010-09-21 46 views
0

我真是關閉此時間;)XSL不必要的下一個元素 - 讀取標籤和子標籤屬性

婁有:

  1. 我XSL腳本
  2. 我的XML文件(即通話腳本)

  3. 在xsl的操作的對XML的輸出(如shuold是)

我的問題是,儘管href和src值應該是相同的,但它們之間始終存在一個 圖片差異(pic3和pic4),就好像我在調用下一個pic元素一樣......但我不明白我在哪裏去做?

<xsl:for-each select="data/pics/pic"> 
     <div>     
      <a> 
       <xsl:attribute name="href"> 
        <xsl:value-of select="@href" /> 
       </xsl:attribute> 
       <img> 
        <xsl:attribute name="src"> 
         <xsl:value-of select="@src" /> 
        </xsl:attribute> 

       </img> 
      </a> 
     </div> 
    </xsl:for-each> 

這是我的XML

<data> 
    <pics> 
     <pic href="pics\pic05.jpg" src="pics\pic05.jpg"></pic> 
     <pic href="pics\pic04.jpg" src="pics\pic04.jpg"></pic> 
     <pic href="pics\pic03.jpg" src="pics\pic03.jpg"></pic> 
     <pic href="pics\pic02.jpg" src="pics\pic02.jpg"></pic> 
     <pic href="pics\pic01.jpg" src="pics\pic01.jpg"></pic> 
    </pics> 
</data> 

這應該是輸出:

<div> 
    <a href="pics\pic01.jpg"> 
     <img src="pics\pic01.jpg"> 
    </a> 
</div> 
<div> 
    <a href="pics\pic02.jpg"> 
     <img src="pics\pic02.jpg"> 
    </a> 
</div> 
<div> 
    <a href="pics\pic03.jpg"> 
     <img src="pics\pic03.jpg"> 
    </a> 
</div> 
<div> 
    <a href="pics\pic04.jpg"> 
     <img src="pics\pic04.jpg"> 
    </a> 
</div> 
<div> 
    <a href="pics\pic05.jpg"> 
     <img src="pics\pic05.jpg"> 
    </a> 
</div> 
+2

爲我工作。我已將您的for-each包含在中。使用xsltproc(--version使用libxml 20706,libxslt 10126和libexslt 815)它會以相反的順序(從pic5到pic1,與源xml的順序相同)生成預期結果。嘗試創建一個最小但完整的(有效!)xsl模板來顯示您的問題。 – rincewind 2010-09-21 15:28:06

+0

同上rincewind說。同時發佈您從XSL樣式表中獲得的實際XML輸出。 – LarsH 2010-09-21 16:22:59

+0

看到我的答案,報告提供的轉換實際上產生了想要的結果。 – 2010-09-21 16:37:54

回答

2

您正在使用可能有錯誤,或者你沒有提供的XSLT處理器真正的xslt代碼和xml文檔。

包括在一個完整的樣式表時所提供的XSLT代碼片斷:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
<xsl:template match="/"> 
    <html> 
    <xsl:for-each select="data/pics/pic"> 
      <div> 
       <a> 
        <xsl:attribute name="href"> 
         <xsl:value-of select="@href" /> 
        </xsl:attribute> 
        <img> 
         <xsl:attribute name="src"> 
          <xsl:value-of select="@src" /> 
         </xsl:attribute> 
        </img> 
       </a> 
      </div> 
     </xsl:for-each> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

和施加在提供的XML文檔

<data> 
    <pics> 
     <pic href="pics\pic05.jpg" src="pics\pic05.jpg"></pic> 
     <pic href="pics\pic04.jpg" src="pics\pic04.jpg"></pic> 
     <pic href="pics\pic03.jpg" src="pics\pic03.jpg"></pic> 
     <pic href="pics\pic02.jpg" src="pics\pic02.jpg"></pic> 
     <pic href="pics\pic01.jpg" src="pics\pic01.jpg"></pic> 
    </pics> 
</data> 

產生想要的,正確的結果

<html> 
    <div> 
     <a href="pics\pic05.jpg"> 
      <img src="pics\pic05.jpg"/> 
     </a> 
    </div> 
    <div> 
     <a href="pics\pic04.jpg"> 
      <img src="pics\pic04.jpg"/> 
     </a> 
    </div> 
    <div> 
     <a href="pics\pic03.jpg"> 
      <img src="pics\pic03.jpg"/> 
     </a> 
    </div> 
    <div> 
     <a href="pics\pic02.jpg"> 
      <img src="pics\pic02.jpg"/> 
     </a> 
    </div> 
    <div> 
     <a href="pics\pic01.jpg"> 
      <img src="pics\pic01.jpg"/> 
     </a> 
    </div> 
</html> 

當使用幾個不同的XSLT處理器(如MSXML3,4,6,Saxon6.5和AltovaXML(XML-SPY))執行此轉換時,會產生相同的結果。

請注意,該變換可以重構爲一個更短,可讀之一:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
<xsl:template match="/"> 
    <html> 
    <xsl:for-each select="data/pics/pic"> 
      <div> 
       <a href="{@href}"> 
        <img src="{@src}"/> 
       </a> 
      </div> 
     </xsl:for-each> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 
+0

+1這可能是一個緩存問題,但我喜歡你的代碼 – Asaf 2010-09-21 17:35:10

+0

對於屬性值模板建議。 – 2010-09-21 17:45:09