Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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.
 
Old February 1st, 2011, 05:09 PM
Friend of Wrox
 
Join Date: Nov 2007
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
 
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.
 
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?
 
Old February 2nd, 2011, 07:16 AM
Friend of Wrox
 
Join Date: Nov 2007
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





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





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