DevStarlight
DevStarlight

Reputation: 804

replicate xmlt tag after transformation

I'm trying to apply to my xml file a transformation having my xsl.

This is my xml.

<?xml version="1.0" encoding="UTF-8"?>
<messages>
  <message>
    <from>Pepe ([email protected])</from>
    <to>Juan ([email protected])</to>
    <datetime>28/02/2011 17:48:23,61</datetime>
    <text>¿Hello, Johnn, what's up?</text>
  </message>
  <message>
    <from>Juan ([email protected])</from>
    <to>Pepe ([email protected])</to>
    <datetime>28/02/2011 17:54:20,87</datetime>
    <text>Here, learning <strong>XML</strong></text>
  </message>
</messages>

And this is my xsl code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <xsl:for-each select="messages/message">
          <from>
              <xsl:value-of select="from"/>
          </from>
          <to>
              <xsl:value-of select="to"/>
          </to>
          <text>
              <xsl:value-of select="text"/>
              <strong>
                <xsl:value-of select="text/strong"/>
              </strong>
          </text>
        </xsl:for-each>
      </body>    
    </html>
  </xsl:template>
</xsl:stylesheet>

Everything is perfect, unless the <text><strong></strong></text>. The problem comes when I do the XML transformation getting this result which is wrong:

<text>Here, learning XML<strong>XML</strong></texto>

For any reason i get replicated XML out of the <strong> tag and i don't know where's the mistake.

Thanks in advice.

Upvotes: 0

Views: 138

Answers (2)

Agnes
Agnes

Reputation: 674

xsl:value-of gives you the string value of the given node, that is, all the text nodes under the given node. That is why you see the "XML" text duplicated - it is part of the string value of both <text> and <strong>.

Upvotes: 0

Linga Murthy C S
Linga Murthy C S

Reputation: 5432

And, even better would have been:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <html>
        <body>
            <xsl:for-each select="messages/message">
                <xsl:copy-of select="from"/>
                <xsl:copy-of select="to"/>
                <xsl:copy-of select="text"/>
            </xsl:for-each>
        </body>    
    </html>
</xsl:template>

Upvotes: 1

Related Questions