Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xsl:attribute not showing 1

Status
Not open for further replies.

Craftor

Programmer
Feb 1, 2001
420
NZ
Hi all,

Here is a snippet from my XSL file:

Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:template match="mytemplate" xml:space="preserve">
    <div>
      <xsl:attribute name="class">
        newsbyline
      </xsl:attribute>
      Your text here
    </div>
  </xsl:template>	
</xsl:stylesheet>

The class attribute isn't being applied to the div element - the div shows up fine in the output.

Can anyone help me with this, please?

Thanks as always

Craftor
:cool:
 
Using xml:space="preserve" is somewhat tricky here. Sometimes the trickiness is sometimes due to the lack of complete warning/error message to noticing users.

[1] If you insist "preserve", this is done like this.
[tt]
<div[highlight]><[/highlight]xsl:attribute name="class"[highlight]>newsbyline<[/highlight]/xsl:attribute>
Your text here
</div>
[/tt]
Note the lack of whitespace after <div> and before <xsl:attribute name="class"> tags, and within the <xsl:attribute>'s content.

[2] The initial failure is that whitespace is being significant here and that those whitespaces after <div> will be taken as a child text node of <div>. Now, as a consequence, the engine is taken as instruction to add an attribute "class" to a text node which as a matter of course won't do.

[3] Unless it is of a real need, use xml:space="preserve" is not a very good idea. If you need to control spacing, use instead <xsl:text>...</xsl:text> for the purpose.
 
Thank you tsuji that solved the problem!

Craftor
:cool:
 
Hi i also have problem show attribute in xml output.


----------------------------------------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="xml"/>
<xsl:template match="/">
<MY_PHONEBOOK>
<xsl:apply-templates select="//FRIEND" />
<xsl:apply-templates select="//NAME" />
<xsl:apply-templates select="//NUMBER" />
</MY_PHONEBOOK>
</xsl:template>
<xsl:template match="FRIEND">
<FRIEND>
</FRIEND>
</xsl:template>
<xsl:template match="NAME">
<NAME>Soli Katir </NAME>
</xsl:template>

<xsl:template match="FREIND">
<NUMBER>

<xsl:attribute name="Cell">73773737</xsl:attribute>
</NUMBER>


</xsl:template>

</xsl:stylesheet>
 
You may have the problem of displaying the attribute, but it is only due to your not taking the work to some degree of seriousness.
[tt]
[red]<!--[/red] <xsl:template match="FREIND"> [red]-->[/red]
[blue]<xsl:template match="[/blue][red]NUMBER[/red][blue]">[/blue]
<NUMBER>
<xsl:attribute name="Cell">73773737</xsl:attribute>
</NUMBER>
</xsl:template>
[/tt]
If you have other questions, you have to open your own thread. It has nothing to do with what discussed in this thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top