2010-09-28 7 views
2

J'ai un xml du format suivantAccès XML en utilisant XSLT

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

Je peux utiliser xslt pour obtenir la valeur de l'élément en utilisant les éléments suivants:

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

Mais, je suis en train de comprendre le code xsl pour lire le xml dans le format suivant:

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

Comment faire? Et si quelqu'un peut poster un lien tutoriel xslt, il sera très apprécié.

Merci à l'avance

+0

Bonne question (+1). Voir ma réponse pour une solution complète. :) –

+2

Voulez-vous dire qu'à la place de l'élément enfant, vous voulez sélectionner un attribut? Ensuite, vous avez besoin de l'axe 'attribute' ou de sa forme abrégée' @ '. Exemple: '' –

Répondre

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. 

Cette transformation:

<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> 

lorsqu'il est appliqué sur ce document XML:

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

pro duces le résultat recherché:

CD1 name 
CD2 name 
CD3 name 

Pour des tutoriels et des livres, voir ma réponse à cette question:

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

1

Un autre site qui est utile pour des tutoriels est: link text

Questions connexes