2010-09-28 141 views
2

我有以下格式訪問XML使用XSLT

<catalog> 
<cd> 
    <title>CD name</title> 
</cd> 
</catalog> 

我可以使用XSLT使用以下來獲取元素值的XML:

<xsl:template match="/"> 
<xsl:for-each select="catalog/cd"> 
<xsl:value-of select="title" /> 
</xsl:for-each> 

但是,我想弄清楚用以下格式讀取xml的xsl代碼:

<catalog> 
<cd title="CD name"/> 
</catalog> 

我該如何做?如果任何人都可以發佈一些xslt教程鏈接,將非常感激。

在此先感謝

+0

好問題(+1)。查看我的答案獲得完整的解決方案。 :) – 2010-09-28 21:53:37

+2

你的意思是代替你想要選擇一個屬性的子元素嗎?那麼你需要'屬性'軸或其縮寫形式'@'。例如:'' – 2010-09-28 22:08:41

回答

3
I have an xml of the following format 

    <catalog> 
    <cd> 
     <title>CD name</title> 
    </cd> 
    </catalog> 

I can use xslt to get the element value using the following: 

    <xsl:template match="/"> 
    <xsl:for-each select="catalog/cd"> 
    <xsl:value-of select="title" /> 
    </xsl:for-each> 

But, I am trying to figure out the xsl code to read the xml in the following format: 


    <catalog> 
    <cd title="CD name"/> 
    </catalog> 

How do I do this? And if anyone can post some xslt tutorial link, it will be much appreciated. 

這種轉變

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:template match="cd"> 
    <xsl:value-of select="concat(@title, '&#xA;')"/> 
</xsl:template> 
</xsl:stylesheet> 

當這個XML文檔施加:

<catalog> 
    <cd title="CD1 name"/> 
    <cd title="CD2 name"/> 
    <cd title="CD3 name"/> 
</catalog> 

親duces想要的結果

CD1 name 
CD2 name 
CD3 name 

對於教程和書籍見我回答這個問題

https://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589

1

,對於教程是有用的另一個網站是: link text