Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 1st, 2011, 05:02 PM
Registered User
 
Join Date: Jan 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default Many text node

Hi,

I have a xml like this

<comment>//<text>************</text>otro texto</comment>
<comment>//<text> PROYECTO: prueba</text></comment>
<comment>//<text>************</text>mas texto</comment>

I need a output like this:

<span class="comment">//<span class="text">********</span>otro texto</span>
<span class="comment">//<span class="text"> PROYECTO: prueba</span></span>
<span class="comment">//<span class="text">********</span>mas texto</span>

I have this:
<xsl:template match="text">
<span class="text">
<xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="comment">
<span class="comment">
<xsl:value-of select="text()"/>
<xsl:apply-templates select="text"/>
<xsl:value-of select="text()[last()]"/>
</span>
</xsl:template>

It is works, but not always I have two text node for a node element. How can improve this code.

Thanks.
Reply With Quote
  #2 (permalink)  
Old February 1st, 2011, 05:09 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Throw out any xsl: value-of and simply put
Code:
  <xsl:apply-templates/>
into each span element, that way you will get the results you want (as there is a built-in template copying text nodes.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #3 (permalink)  
Old February 1st, 2011, 05:59 PM
Registered User
 
Join Date: Jan 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks so much.
Reply With Quote
  #4 (permalink)  
Old February 2nd, 2011, 06:33 AM
Registered User
 
Join Date: Jan 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default xsl:apply-templates select="*"

I have a xml like this
<class>class <name>PAL_c</name>
<block>{<private type="default">
</private><public>public:
<comment>//<text> Leer una entrada</text></comment>
<function_decl><type><name>void</name></type> <name>PAL_Leer</name> <parameter_list>(<param><decl><type><name>PAL_Sepa rador_t</name>*</type> <name>separador_p</name></decl></param>, <param><decl><type><name>bool</name>*</type> <name>finDeFichero_p</name></decl></param>)</parameter_list>;</function_decl>
</public><private>private:
<decl_stmt><decl><type><name>char</name></type> <name><name>PAL_texto</name><index>[<expr><name>PAL_MAXCAR</name><operator>+</operator>1</expr>]</index></name></decl>;</decl_stmt>
</private>}</block>;</class>

The need an output like this:

<span class="class"><span class="key">class </span><span class="name">PAL_c</span>
<span class="block">{<span class="private"><span class="key">
</span></span><span class="public"><span class="key">public:
</span><span class="comment">//<span class="text"> Leer una entrada</span></span>
<a name="PAL_Leer"></a><span class="function_decl"><span class="type"><span class="name">void</span></span> <span class="name">PAL_Leer</span> <span class="parameter_list">(<span class="param"><span class="decl"><span class="type"><span class="name">PAL_Separador_t</span>*</span> <span class="name">separador_p</span></span></span>, <span class="param"><span class="decl"><span class="type"><span class="name">bool</span>*</span> <span class="name">finDeFichero_p</span></span></span>)</span>;</span>

<span class="private"><span class="key">private:
</span><
span class="decl_stmt"><span class="decl"><span class="type"><span class="name">char</span></span> <span class="name"><span class="name">PAL_texto</span><span class="index">[<span class="expr"><span class="name">PAL_MAXCAR</span><span class="operator">+</span>1</span>]</span></span></span>;</span> <span class="comment">//<span class="text"> texto, terminado en nulo</span></span>
</span>}</span>;</span>

My xsl is like this:

<xsl:template match="class">
<xsl:variable name="clase" select="name"/>
<span class="class">
<xsl:call-template name="key"/>
<xsl:apply-templates />
</span>
</xsl:template>

<xsl:template name="key">
<span class="key"><xsl:value-of select="text()"/></span>
</xsl:template>

But with my xsl, for example the node "class" show 2 times:

<span class="class"><span class="key">class </span>class <span class="name">PAL_c</span>
<span class="block">{<span class="private"><span class="key">
</span></span><span class="public"><span class="key">public:

</span>public: ...........................

I tested with <xsl:apply-templates select="*" /> insted of <xsl:apply-templates /> , but with "*" not shows the other node text().


Somebody can help me? I do not what to do?
Reply With Quote
  #5 (permalink)  
Old February 2nd, 2011, 07:16 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Please indent your code samples and mark them up with http://p2p.wrox.com/misc.php?do=bbcode#code to make them more readable.

I am not sure I understand what you want to achieve but perhaps
Code:
<xsl:template match="class">
  <span class="class">
    <xsl:apply-templates/>
  </span>
</xsl:template>

<xsl:template match="class/text()[1]">
  <span class="key">
    <xsl:value-of select="."/>
  </span>
</xsl:template>
helps.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Node text() mejias XSLT 1 January 28th, 2011 06:40 PM
Searching text within a node LeeSir XSLT 3 August 6th, 2009 09:07 AM
Displaying data as a Parent Node with Left Node and Right Node Manoj Bisht Visual Basic 2008 Professionals 0 April 2nd, 2009 02:34 AM
substitute text() node value in XPath expression Coolcampers XSLT 4 April 30th, 2008 08:39 AM
How to select a node or text from treeview MacDevv C# 2005 3 September 5th, 2006 05:23 AM



All times are GMT -4. The time now is 06:19 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.