2013-05-19 56 views
-1

我有一個非標準html元素的XML文件,如下所示: 注意:段落已縮短。XSLT拷貝但更改值

    <content> 
         Sacrifice. It's Mass Effect 3's major theme... 
         <p /> 
         Mass Effect was about time and place; you d... 
         <p /> 
         Mass Effect 3 is focused more on plot th... 
         <p /> 
         <img url="me3_img1.jpg">Plenty of games feat...</img> 
         Like Star Wars, Mass Effect 3 is an inc... 
         <p /> 
         The reapers aren't your only adver... 
         <p /> 
         <img url="me3_img2.jpg">If looks could kill..</img> 
         The series' focus on player choice is as vi... 
         <p /> 
         This intense narrative is met wi... 
         <p /> 
         <img url="me3_img3.jpg">The sun is sett...</img> 
         These are exquis... 
       </content> 

每個段落之間會有差距。 每張圖片都必須出現在內容中的段落之間,同時必須有一個標題,內容介於之間和 請注意,有一個網址,而不是一個src,所以這必須改變。

我目前的xslt只有copy-of和css修復了佈局,但img沒有顯示,因爲我無法將url更改爲src,並且設置了標題。

請幫忙。

更新:

<xsl:for-each select='./review/content/child::*'> 
          <xsl:value-of select="name()"/> 
          <xsl:choose> 
          <xsl:when test="name()='p'"> 
           <p /> 
          </xsl:when> 
          <xsl:when test="name()='img'"> 
           <div class="reviewImage"> 
           <img> 
            <xsl:attribute name="src"> 
            <xsl:value-of select="./@url"/> 
            </xsl:attribute> 
           </img> 
           <xsl:value-of select="."/> 
           </div>         
          </xsl:when> 
          <xsl:otherwise> 
           <xsl:value-of select="."/> 
          </xsl:otherwise> 
          </xsl:choose> 
         </xsl:for-each> 

它不outputing任何文本的()。圖片和標題現在排序。

發現

解決方案:

     <xsl:for-each select='./review/content/node()'> 
          <xsl:choose> 
          <xsl:when test="name()='p'"> 
           <p /> 
          </xsl:when> 
          <xsl:when test="name()='img'"> 
           <div class="reviewImage"> 
           <img> 
            <xsl:attribute name="src"> 
            <xsl:value-of select="./@url"/> 
            </xsl:attribute> 
           </img> 
           <xsl:value-of select="."/> 
           </div>         
          </xsl:when> 
          <xsl:otherwise> 
           <xsl:value-of select="."/> 
          </xsl:otherwise> 
          </xsl:choose> 
         </xsl:for-each> 

回答

0

copy-of點是從輸入樹拷貝內容不變到輸出樹。

爲了改變輸入和輸出樹之間的東西,你需要使用apply-templatestemplate

如果您希望在輸出樹上輸出與輸入樹相同名稱但內容不同的元素,則可以在模板中使用copy,並使用適當的內容填充「副本」。

+0

我將如何再拆

甚至跟蹤的每個圖像屬於 – Gerardlamo

+0

我不知道你所說的「分裂p」是什麼意思,? 'copy-of'也不會這樣做,但你可能會這樣做在模板上匹配'p'和「分割」它。 –

+0

您不需要跟蹤圖像所屬的位置。默認情況下,'apply-templates'以文檔順序對所選節點集進行操作,這樣就會發生。 –

0

當您想要複製某些內容並更改其中的一部分時,答案几乎總是與其他模板一起使用標識模板。 copy-of不能這樣做,我儘可能避免copy-of

這裏是你如何能做到你的描述(當你說「標題」,我不知道你是否意味着alttitle屬性,所以我會同時使用:

<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="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="content/img"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 
     <xsl:attribute name="alt"> 
     <xsl:value-of select="."/> 
     </xsl:attribute> 
     <xsl:attribute name="title"> 
     <xsl:value-of select="."/> 
     </xsl:attribute> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="content/img/@url"> 
    <xsl:attribute name="src"> 
     <xsl:value-of select="." /> 
    </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

當這是在你的樣品輸入運行,其結果是:

<content> 
    Sacrifice. It's Mass Effect 3's major theme... 
    <p /> 
    Mass Effect was about time and place; you d... 
    <p /> 
    Mass Effect 3 is focused more on plot th... 
    <p /> 
    <img src="me3_img1.jpg" alt="Plenty of games feat..." title="Plenty of games feat..." /> 
    Like Star Wars, Mass Effect 3 is an inc... 
    <p /> 
    The reapers aren't your only adver... 
    <p /> 
    <img src="me3_img2.jpg" alt="If looks could kill.." title="If looks could kill.." /> 
    The series' focus on player choice is as vi... 
    <p /> 
    This intense narrative is met wi... 
    <p /> 
    <img src="me3_img3.jpg" alt="The sun is sett..." title="The sun is sett..." /> 
    These are exquis... 
</content> 
+0

url尚未更改爲src。 – Gerardlamo

+0

對不起,忘了。現在應該是好的。 – JLRishe