how to view images with xsl with src from xml file img

Asked

Viewed 899 times

2

I have for example the XSL code:

<xsl:for-each select="//*">
  <xsl:for-each select="imagem">
    <img>
      <xsl:attribute name="src">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </img>
  </xsl:for-each>
</xsl:for-each> 

have for example following xml code:

<?xml version="1.0" ?>
<imagens>
<imagem>
    <png id="1">img1.png</png>
    <png id="2">img2.png</png>
    <png id="3">img3.png</png>
    <png id="4">img4.png</png>
    <png id="5">img5.png</png>
</imagem>
<imagem>
    <png id="1">img6.png</png>
    <png id="2">img7.png</png>
    <png id="3">img8.png</png>
    <png id="4">img9.png</png>
    <png id="5">img10.png</png>
</imagem>

I’m trying to get this code to turn into something like this:

<img src="img1.png"><img src="img2.png"><!--E assim por diante-->

But he stays that way:

<img src="img1.pngimg2.pngimg3.pngimg4.pngimg5.png">

someone and has some idea how to solve?

2 answers

1

The ideal is to select the element you want using a template that will be called recursively while document nodes are processed. The XSL document below does exactly what you want without using for-each:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="png">
        <img src="{.}" />
    </xsl:template>

</xsl:stylesheet>

Upshot:

<img src="img1.png"><img src="img2.png"> ... <img src="img10.png">
  • Vlw was just like that. I just don’t give +1 to the answer pq to no reputation kkk

0

Try the following:

  <xsl:for-each select="//**">
    <tr>
      <td>
        <xsl:value-of select="imagem"/>
      </td>
      <td>
        <xsl:element name="imagem">
          <xsl:attribute name="src">
            <xsl:value-of select="png"/>
          </xsl:attribute>
          <xsl:attribute name="align">left</xsl:attribute>
        </xsl:element>
      </td>
    </tr>
  </xsl:for-each>
</table>

  • Not yet so. Generated a code similar to img1.pngimg2.png... without the images and the rest img will appear with empty src

  • i tried something like this and almost worked just missed tab and Kda executed code snippet from xml image generates an error

  • <table border="1"><xsl:for-each select="//*"><tr><xsl:element name="image"><td><img><xsl:attribute name="src"><xsl:value-of select="." /></xsl:attribute></img></td></xsl:element></xsl:for-each></table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.