sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
mickey0's Avatar

xsl:foreach


Question posted by: mickey0 (Member) on July 10th, 2008 12:10 AM
Hello,
I just this question:
Expand|Select|Wrap|Line Numbers
  1.     <xsl:for-each select="//tag1">
  2.       <xsl:value-of select="text()" />
  3.     </xsl:for-each>

my output is:
hello;world;
I need:
Hello;
World;

my xslt header is:
Expand|Select|Wrap|Line Numbers
  1.        <xsl:output method="text"                 
  2.                    encoding="iso-8859-1"           
  3.                    indent="yes"                

My output file is a text file.....
How can I do this, please?

Best regards,
16 Answers Posted
roces's Avatar
roces July 10th, 2008 09:19 AM
Newbie - 7 Posts
#2: Re: xsl:foreach

You can concatenate text() with line feed entity "&#xD;".

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/">
  2.    <xsl:for-each select="//tag1">
  3.      <xsl:value-of select="concat(text(), '&#xD;')"/>
  4.    </xsl:for-each>
  5. </xsl:template>
mickey0's Avatar
mickey0 July 10th, 2008 12:08 PM
Member - 88 Posts
#3: Re: xsl:foreach

hi thanks,
what do you think if I add it inside a text tag? is this better or not?
Expand|Select|Wrap|Line Numbers
  1. <xsl:text>>&#xD;</xsl:text> 

sometimes I need to do this too:
Expand|Select|Wrap|Line Numbers
  1.  <xsl:text>>printf("hello");&#xD;</xsl:text>  

or indent the braces:
Expand|Select|Wrap|Line Numbers
  1.  <xsl:text>>         } &#xD;</xsl:text>  

What do you suggest for these aims?

Thanks,
roces's Avatar
roces July 10th, 2008 03:02 PM
Newbie - 7 Posts
#4: Re: xsl:foreach

I think it's absolutely normal, but variant with concatenation is more readable than your. Compare:

Expand|Select|Wrap|Line Numbers
  1. <xsl:value-of select="concat( 'your_text', text(), '>         }  ', some_tag_value, 'your_text2')"/>


and

Expand|Select|Wrap|Line Numbers
  1. <xsl:text>your_text</xsl:text><xsl:value-of select="text()/><xsl:text>>         }  </xsl:text><xsl:value-of select="some_attrib_value"/><xsl:text>your_text2</xsl:text>
mickey0's Avatar
mickey0 July 10th, 2008 07:39 PM
Member - 88 Posts
#5: Re: xsl:foreach

thanks,
seem ok, but the function concat(), where I can find explanation about other function too?
Why doens't these below work?
Expand|Select|Wrap|Line Numbers
  1.              indent="yes"           
  2.               xalan:indent-amount="2"/>
roces's Avatar
roces July 11th, 2008 01:18 PM
Newbie - 7 Posts
#6: Re: xsl:foreach

It's xalan specific instruction. I think U need to read some docs about it. )
mickey0's Avatar
mickey0 July 11th, 2008 04:58 PM
Member - 88 Posts
#7: Re: xsl:foreach

Thanks; a further question if possible....
The text in the xml contains this string inside a tag: "hello\nworld" (and I mean not the new line but the '\' follow by 'n'
Expand|Select|Wrap|Line Numbers
  1. <xsl:value-of select="concat( 'PRINT(', text(), ');'  /> 
  2. //produce in the output file:
  3. PRINT(hello\nworld);

while I need in the output text file:
Expand|Select|Wrap|Line Numbers
  1. PRINT(hello);
  2. PRINT(world);

How can I do this, please?
roces's Avatar
roces July 14th, 2008 08:31 AM
Newbie - 7 Posts
#8: Re: xsl:foreach

<xsl:value-of select="concat( 'PRINT(',
replace(text(), '\n', &#xD;),
');' />
mickey0's Avatar
mickey0 July 14th, 2008 10:25 AM
Member - 88 Posts
#9: Re: xsl:foreach

Quote:
Originally Posted by roces
<xsl:value-of select="concat( 'PRINT(',
replace(text(), '\n', &#xD;),
');' />

hello,

it seems there's a problem to put the "(" ")" of replace inside a concat......Do you know it? Is there any trick, please?

thanks,
roces's Avatar
roces July 15th, 2008 09:34 AM
Newbie - 7 Posts
#10: Re: xsl:foreach

Expand|Select|Wrap|Line Numbers
  1. <xsl:value-of select="concat( 'PRINT(', 
  2.                                                             replace(text(), '\n',  ), 
  3.                                                           ');' "/>

I forgot double quote at the end of "select" attribute. And you just need to pay attention to the code and read more docs ))
mickey0's Avatar
mickey0 July 15th, 2008 10:33 PM
Member - 88 Posts
#11: Re: xsl:foreach

Quote:
Originally Posted by roces
Expand|Select|Wrap|Line Numbers
  1. <xsl:value-of select="concat( 'PRINT(', 
  2.                                                             replace(text(), '\n',  ), 
  3.                                                           ');' "/>

I forgot double quote at the end of "select" attribute. And you just need to pay attention to the code and read more docs ))

Hello, I don't understand: my code is this
Expand|Select|Wrap|Line Numbers
  1.    <xsl:value-of select="concat('       doc.print(', 
  2.     replace(text(), '\n', '&#xD;' ), ');&#xD;');" disable-output-escaping="yes" />

now I'm editing it with .Net and it says that replace() isn't xslt function....
Is it ok ,please?
jkmyoung's Avatar
jkmyoung July 16th, 2008 01:30 AM
Moderator - 869 Posts
#12: Re: xsl:foreach

Guessing you're using XSLT 1.0 then.
You could use a recursive replace function:

http://www.xml.com/pub/a/2002/06/05/transforming.html
for some reason, it won't let me paste the function....
mickey0's Avatar
mickey0 July 19th, 2008 06:46 PM
Member - 88 Posts
#13: Re: xsl:foreach

mm I haven't still done this thing; I can't understand how to do the replace() (in my case) with xslt 1.0....any suggests....
I remember...my output file output:
Expand|Select|Wrap|Line Numbers
  1. print(hello\nworld); //"hello\nworld" is the result of text() xslt function

Instead I need:
Expand|Select|Wrap|Line Numbers
  1. print(hello);
  2. print(world);

That is: xslt should looks into text() and break the string when it sees a '\n';

thanks.
jkmyoung's Avatar
jkmyoung July 21st, 2008 02:46 PM
Moderator - 869 Posts
#14: Re: xsl:foreach

Could you post your input? It might really help to clear things up. I feel like we're forcing an error down a crooked path.
mickey0's Avatar
mickey0 July 22nd, 2008 12:40 AM
Member - 88 Posts
#15: Re: xsl:foreach

Expand|Select|Wrap|Line Numbers
  1. <root>
  2.     <htmltag><![CDATA[<html>\n<body>]]></htmltag>        
  3.     <text><![CDATA[This is a text node]]></text>    
  4.     <htmltag><![CDATA[.\n</body>\n</html>]]></htmltag>
  5. </root>

This is my xml; with xalan and a xslt (as you read before) I have to produce:
Expand|Select|Wrap|Line Numbers
  1. print(<html>);
  2. print(<body>);
  3. ...................

at the moment it produce:
Expand|Select|Wrap|Line Numbers
  1. print(<html>\n<body>);

THe problem is that '\n' (and at this point the fact that xalan use xslt 1.0 and doens't know replace() )

Are you understand? I dont' understand how write the replace in xslt 1.0....
jkmyoung's Avatar
jkmyoung July 22nd, 2008 02:32 AM
Moderator - 869 Posts
#16: Re: xsl:foreach

Expand|Select|Wrap|Line Numbers
  1. <xsl:template name="globalReplace">                           
  2.   <xsl:param name="outputString"/>                            
  3.   <xsl:param name="target"/>                                  
  4.   <xsl:param name="replacement"/>                             
  5.   <xsl:choose>                                                
  6.     <xsl:when test="contains($outputString,$target)">         
  7.       <xsl:text>print(<xsl:text>
  8.       <xsl:value-of select=                                   
  9.         "concat(substring-before($outputString,$target),      
  10.                $replacement)"/>                               
  11.       <xsl:text>);<xsl:text>
  12.       <xsl:call-template name="globalReplace">                
  13.         <xsl:with-param name="outputString"                   
  14.              select="substring-after($outputString,$target)"/>
  15.         <xsl:with-param name="target" select="$target"/>      
  16.         <xsl:with-param name="replacement"                    
  17.              select="$replacement"/>                          
  18.       </xsl:call-template>                                    
  19.     </xsl:when>                                               
  20.     <xsl:otherwise>              
  21.       <xsl:text>print(<xsl:text>                             
  22.       <xsl:value-of select="$outputString"/>                  
  23.       <xsl:text>);<xsl:text>
  24.     </xsl:otherwise>                                          
  25.   </xsl:choose>                                               
  26. </xsl:template>                     


Put the corresponding print text around anywhere where you output the text.

target is '\n', replacement is '' (empty string)
mickey0's Avatar
mickey0 July 23rd, 2008 12:26 AM
Member - 88 Posts
#17: Re: xsl:foreach

hello,

there is a problem: $outputString; I suppose I have to set this via Xalan (in java code); but as you can see in my previous code I use text();
With xalan I can do :
trasformer.setParameter("target", '\n' );
trasformer.setParameter("replacement", "" );
but only this and not other...

Are you understand, plese?
Reply
Not the answer you were looking for? Post your question . . .
197,034 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,034 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top XML Contributors