2010-11-02 55 views
5

我正在使用XSLT和XML。如何使用xslt從XML讀取註釋並在元素中添加註釋文本

我和我有下面的XML。

<?xml version="1.0" encoding="UTF-8"?> 
<mappings> 
    <!-- News mapping --> 
    <mapping old="mbp" new="/SessionHandler.aspx?pageurl=/BP.aspx&amp;pub=/english&amp;section=IBE&amp;j=f"/> 
    <mapping old="about/news" new="about/news/news.aspx"/> 
    <!-- CUGO's--> 
    <mapping old="/nhs" new="/cugo.aspx?promoCode=UKNHS01&amp;pub=/uk/english"/> 
    <mapping old="/hk/ukstudentfare" new="/cugo.aspx?promoCode=HKSTU10&amp;pub=/hk/Chinese"/> 
    <!-- Reserved below vanity URL's --> 
    <mapping old="/kgfmastercard" new=""/> 
    <mapping old="/mastercard" new=""/> 
    <!-- Other vanity URL's--> 
    <mapping old="/destinationbriefs" new="http://www.ekgroup.com/destinationbriefs"/> 
    <mapping old="/win" new="/ch/german/destinations_offers/win_two_tickets_on_A380.aspx"/> 
    <!--FIFA Fan Fest--> 
    <mapping old="/romefanfest" new="/it/italian/destinations_offers/rome_international_fifa_fan_fest.aspx"/> 
    <mapping old="/parisfanfest" new="/fr/french/destinations_offers/paris_international_fifa_fan_fest.aspx"/> 
</mappings> 

下面是XSLT我使用

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="xsl fo"> 
    <xsl:output method="xml" indent="yes" /> 
    <xsl:template match="/"> 
    <xsl:for-each select="mappings/mapping"> 
     <VanityUrl> 
     <old> 
      <xsl:value-of select="@old" /> 
     </old> 
     <new> 
      <xsl:value-of select="@new" /> 
     </new> 
     <dateAdded>2010-05-03T14:45:00</dateAdded> 
     <xsl:if test="@new = ''"> 
      <NotLive>Yes</NotLive> 
     </xsl:if> 
     <xsl:if test="preceding-sibling::comment()"> 
      <comments> 
      <xsl:value-of select="preceding-sibling::comment()" /> 
      </comments> 
     </xsl:if> 
     </VanityUrl> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

現在我想獲得結果如下圖所示:

<VanityUrl> 
     <old>/mbp</old> 
     <new>/SessionHandler.aspx?pageurl=/BP.aspx&amp;pub=/english&amp;section=IBE&amp;j=f</new> 
     <dateAdded>2010-05-03T14:45:00 </dateAdded> 
     <comment>News mapping</comments>   
</VanityUrl> 
<VanityUrl> 
     <old>about/news</old> 
     <new>about/news/news.aspx</new> 
     <dateAdded>2010-05-03T14:45:00 </dateAdded> 
     <comment>News mapping</comments>   
</VanityUrl> 
<VanityUrl> 
     <old>/nhs</old> 
     <new>/cugo.aspx?promoCode=UKNHS01&amp;pub=/uk/english</new> 
     <dateAdded>2010-05-03T14:45:00 </dateAdded> 
     <comment>CUGO's</comments>  
</VanityUrl> 
<VanityUrl> 
     <old>/hk/ukstudentfare</old> 
     <new>/cugo.aspx?promoCode=HKSTU10&amp;pub=/hk/Chinese</new> 
     <dateAdded>2010-05-03T14:45:00 </dateAdded> 
     <comment>CUGO's</comments>  
</VanityUrl> 
<VanityUrl> 
     <old>/kgfmastercard</old> 
     <new></new> 
     <NotLive>yes</NotLive> 
     <dateAdded>2010-05-03T14:45:00 </dateAdded> 
     <comment>Reserved below vanity URL's</comments>  
</VanityUrl> 
<VanityUrl> 
     <old>/mastercard</old> 
     <new></new> 
     <NotLive>yes</NotLive> 
     <dateAdded>2010-05-03T14:45:00 </dateAdded> 
     <comment>Reserved below vanity URL's</comments>  
</VanityUrl> 

一切都OK了,但我不能工作在評論部分,我如何根據相關評論添加評論元素。我的意思是低於我的XSLT代碼不能正常工作,其加入的第一個評論僅即新聞映射

xsl:if test="preceding-sibling::comment()"> 
       <comments> 
       <xsl:value-of select="preceding-sibling::comment()" /> 
       </comments> 
      </xsl:if> 
+0

謝謝!我正在使用xmlSpy – 2010-11-02 11:38:05

回答

10

preceding-sibling::comment()給你一個節點設置相反文檔順序上的當前級別的所有前面的註釋節點。但是,您需要選擇最接近的評論以使其正常工作:

<xsl:if test="preceding-sibling::comment()[1]"> 
    <comments> 
    <xsl:value-of select="preceding-sibling::comment()[1]" /> 
    </comments> 
</xsl:if> 
+0

謝謝@ 0xA3,它爲我工作乾杯! – 2010-11-02 11:52:38

+0

+1得到正確的答案。 – 2010-11-02 13:02:52

+0

+1正確答案。 – 2010-11-02 14:50:42