2010-09-28 71 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