2010-09-02 22 views
8

Todavía estoy buscando, pero no he encontrado todavía la manera de realizar algo como esto:Xslt que genera Html <IMG> etiquetas. Cómo usar un valor de nodo XML como SRC-atributo para los <IMG> Etiquetas

xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <!-- some other templates --> 
    <xsl:template match="IMAGE"> 
     <img src="src_attribute_to_be_read_from_the_xml_file.jpg"/> 
    </xsl:template>  
</xsl:stylesheet> 

En mi XML <IMAGE> etiquetas, el valor de texto es el nombre de archivo que se debe insertar en la cadena src_attribute_to_be_read_from_the_xml_file.jpg cuando se procesa con este archivo Xslt.

¿Tiene alguna idea para realizar esto?

Respuesta

26

Se utilizan xsl:attribute:

<img> 
    <xsl:attribute name="src"> 
     <xsl:value-of select="you path here"/> 
    </xsl:attribute> 
</img> 

También debe ser posible utilizar

<img src="{some expression here}" /> 

De acuerdo con la specification esto se llama una plantilla valor atributo y siempre debería funcionar (es decir, tanto XSLT 1.0 y 2.0). Bonito. Ahora aprendí algo también.

+1

thx xsl: atributo hace el trabajo. y gracias a la información sobre "{$ some-variable-here}", debería verificar la forma en que funciona. –

+0

Debería. Revisé mi respuesta. – musiKk

+1

@musikk y @Stephane Rolland: Siempre que pueda usar elementos de resultado literales y AVT, úselo. Es rápido y compacto. –

1

Alternativamente, puede utilizar la plantilla XSL:

<xsl:template match="image"> 
<xsl:element name="IMG"> 
    <xsl:attribute name="src"> 
    <xsl:value-of select="your_path"/> 
    </xsl:attribute> 
    <xsl:attribute name="title"> 
    <xsl:value-of select="your_title"/> 
    </xsl:attribute > 
</xsl:element> 

0

Y si desea agregar los height, width, y alt atributos, entonces usted puede hacerlo como lo siguiente:

  <img> 
      <xsl:attribute name="src"> 
       <xsl:value-of select="picture"/> 
       </xsl:attribute> 
       <xsl:attribute name="title"> 
       <xsl:value-of select="pictureTitle"/> 
       </xsl:attribute > 
       <xsl:attribute name="alt"> 
       <xsl:value-of select="pictureTitle"/> 
       </xsl:attribute > 
       <xsl:attribute name="height"> 
       20 
       </xsl:attribute > 
       <xsl:attribute name="width"> 
       30 
       </xsl:attribute > 
     </img> 
Cuestiones relacionadas