2008 04 de abril de 2008

Traballando con atributos nó XML XSLT

Se usar XML e XSL, entón vostede pode ter vindo a través dun tempo, cando ten que xogar con atributos e valores de nós XML de XSL ti. Son unha chea de sitios con información de alento longo sobre iso, pero ningún que eu atope foron breve e preciso ... Esta é ningún tutorial XML / XSL, pero a miña tentativa de pór en conxunto unha especie de fraude lista ...

A mostra XML que imos traballar con esta aparencia ...

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="food.xsl"?>
<food>
<fruits type="tropical">
<item name="mango" moreinfo="http://www.mango.com">Mango is the king of fruits</item>
<item name="banana" moreinfo="http://www.banana.com">Banana once a day , keeps the doctor away</item>
<item name="orange" moreinfo="http://www.orange.com">Orange is the color and rich in vitamin C</item>
<item name="Papaya" moreinfo="http://www.papaya.com">Papaya - Hot when raw, cold when ripe</item>
</fruits>
</food>

Entón imos comezar a transformar o noso XML enriba usando XSL

Exemplo 1: Amosando valor dun atributo seleccionado

<xsl:value-of select="/food/fruits/item[@name='orange']" /><br />
to get more information about <a href="{/food/fruits/item[@name='orange']/@moreinfo}" target="new" ><xsl:value-of select="/food/fruits/item[@name='orange']/@name" /> </a>

Resultado será coma HTML

Orange is the color and rich in vitamin C,
to get more information about <a href="http://www.orange.com" target="new">


Exemplo 2: Percorrer e amosar todos os nomes de atributo XML e os seus valores

<xsl:for-each select="/food/fruits/item/@*">
attribute name : <xsl:value-of select="name()"/>,
attribute value : <xsl:value-of select="."/> <br />
</xsl:for-each>

Resultado será coma HTML


attribute name : name, attribute value : mango
attribute name : moreinfo, attribute value : http://www.mango.com
attribute name : name, attribute value : banana
attribute name : moreinfo, attribute value : http://www.banana.com
attribute name : name, attribute value : orange
attribute name : moreinfo, attribute value : http://www.orange.com
attribute name : name, attribute value : Papaya
attribute name : moreinfo, attribute value : http://www.papaya.com


Descarga de todos os ficheiros anteriores aquí (245 descargas)



Assist a este espazo, eu vou continuar a actualizar esta con novos descubrimentos ...


NDK casa | Expresando IT | Palate expresando | Penmenship expresando | AWE expresando | expresar o meu