2011-10-13 123 views
2

下面是示例XML文件 -僅當網址存在時超鏈接..?

<?xml version="1.0" encoding="UTF-8"?> 
<Catalog> 
<Book> 
    <AName>Steven Holzner</AName> 
    <BName>Using XSLT</BName> 
    <Pub>ABC Publication </Pub> 
    <Web>http://www.ABCPub.com</Web> 
</Book> 
<Book> 
    <AName>Steven Holzner</AName> 
    <BName>Using HTML</BName> 
    <Pub>XYZ Publication </Pub> 
    <Web></Web> 
</Book> 
</Catalog> 

我想如果網絡地址,則其他明智的只是酒吧的名字瓦特/ OA鏈接到超鏈接的酒吧名稱...

我的XSL代碼是在這裏 -

<xsl:template match="Catalog"> 
    <xsl:for-each select="Book"> 
    <p><a> 
    <xsl:attribute name="href"> 
     <xsl:value-of select="Web"/> 
    </xsl:attribute> 

    <xsl:value-of select="Pub"/> 
    </a></p> 
    </xsl:for-each> 
</xsl:template> 

我想下面的HTML源 -

<p> < A HREF =「HT TP://www.ABCPub.com」 > ABC公佈</A > </P > <p> XYZ出版</A > </P >

RightNow公司,這個XSL給超級鏈接標籤,即使瀏覽網址沒有給出.. 什麼代碼將做需要..?

有一個愉快的一天 - 約翰

+1

好問題,+1。 XSLT是一種功能強大而且非常具有表現力的語言,它可以在沒有任何條件,循環結構或單獨的屬性生成指令的情況下,以最少數量的行來解決此問題。 –

回答

1

這可以用很短的和簡單的改造來實現 - 沒有<xsl:for-each>,沒有<xsl:if><xsl:choose><xsl:when>,沒有<xsl:attribute>在所有

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="Book[Web/text()]"> 
    <p> 
     <a href="{Web}"> 
     <xsl:value-of select="Pub"/> 
     </a> 
    </p> 
</xsl:template> 

<xsl:template match="Book"> 
    <p> 
     <xsl:value-of select="Pub"/> 
    </p> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉變是在所提供的XML文檔應用:

<Catalog> 
    <Book> 
     <AName>Steven Holzner</AName> 
     <BName>Using XSLT</BName> 
     <Pub>ABC Publication </Pub> 
     <Web>http://www.ABCPub.com</Web> 
    </Book> 
    <Book> 
     <AName>Steven Holzner</AName> 
     <BName>Using HTML</BName> 
     <Pub>XYZ Publication </Pub> 
     <Web></Web> 
    </Book> 
</Catalog> 

想要的,正確的結果產生

<p> 
    <a href="http://www.ABCPub.com">ABC Publication </a> 
</p> 
<p>XYZ Publication </p> 

說明:模板模式匹配。

+0

最後一些AVT的:-)非常好的Dimitre。 +1 –

+0

@DevNull:不客氣:) –

+0

@Demitre ...非常感謝... :) – John

1

只是測試如果網站是充滿了選擇/時/其他

<xsl:template match="Catalog"> 
    <xsl:for-each select="Book"> 
    <p><xsl:choose> 
<xsl:when test="string(Web)"><a> 
    <xsl:attribute name="href"> 
     <xsl:value-of select="Web"/> 
    </xsl:attribute> 

    <xsl:value-of select="Pub"/> 
    </a> 
</xsl:when> 
<xsl:otherwise> 
<xsl:value-of select="Pub"/> 
</xsl:othwerwise> 
</xsl:choose> 
</p> 
    </xsl:for-each> 
</xsl:template> 
+0

謝謝Nin ...非常感謝親愛的...它的工作.. – John

0
<xsl:if test="Web != ''"> 
    <a> 
    <xsl:attribute name="href"> 
     <xsl:value-of select="Web"/> 
    </xsl:attribute> 
    <xsl:value-of select="Pub"/> 
    </a> 
</xsl:if> 

^將顯示出版商只有在有網絡地址,你想要的是一個xsl:選擇...

<xsl:choose> 
    <xsl:when test="Web != ''"> 
     <a> 
     <xsl:attribute name="href"> 
      <xsl:value-of select="Web"/> 
     </xsl:attribute> 
     <xsl:value-of select="Pub"/> 
     </a> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="Pub"/> 
    </xsl:otherwise> 
</xsl:choose> 
+0

謝謝謝爾多,你的第二個對我來說已經足夠了。第一個可能來救援未來;)..再次感謝.. – John