472,145 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

xsl:foreach

mickey0
142 100+
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>
  4.  
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"                
  4.  
My output file is a text file.....
How can I do this, please?

Best regards,
Jul 9 '08 #1
16 3888
roces
7
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>
Jul 10 '08 #2
mickey0
142 100+
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,
Jul 10 '08 #3
roces
7
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')"/>
  2.  
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>
  2.  
Jul 10 '08 #4
mickey0
142 100+
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"/>
  3.  
Jul 10 '08 #5
roces
7
It's xalan specific instruction. I think U need to read some docs about it. )
Jul 11 '08 #6
mickey0
142 100+
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);
  4.  
while I need in the output text file:
Expand|Select|Wrap|Line Numbers
  1. PRINT(hello);
  2. PRINT(world);
  3.  
How can I do this, please?
Jul 11 '08 #7
roces
7
<xsl:value-of select="concat( 'PRINT(',
replace(text(), '\n', &#xD;),
');' />
Jul 14 '08 #8
mickey0
142 100+
<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,
Jul 14 '08 #9
roces
7
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 ))
Jul 15 '08 #10
mickey0
142 100+
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" />
  3.  
now I'm editing it with .Net and it says that replace() isn't xslt function....
Is it ok ,please?
Jul 15 '08 #11
jkmyoung
2,057 Expert 2GB
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....
Jul 16 '08 #12
mickey0
142 100+
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
  2.  
Instead I need:
Expand|Select|Wrap|Line Numbers
  1. print(hello);
  2. print(world);
  3.  
That is: xslt should looks into text() and break the string when it sees a '\n';

thanks.
Jul 19 '08 #13
jkmyoung
2,057 Expert 2GB
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.
Jul 21 '08 #14
mickey0
142 100+
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>
  6.  
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. ...................
  4.  
at the moment it produce:
Expand|Select|Wrap|Line Numbers
  1. print(<html>\n<body>);
  2.  
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....
Jul 21 '08 #15
jkmyoung
2,057 Expert 2GB
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>                     
  27.  
Put the corresponding print text around anywhere where you output the text.

target is '\n', replacement is '' (empty string)
Jul 22 '08 #16
mickey0
142 100+
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?
Jul 22 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Marcel | last post: by
1 post views Thread by Spam sucks | last post: by
3 posts views Thread by Tjerk Wolterink | last post: by
3 posts views Thread by Tjerk Wolterink | last post: by
2 posts views Thread by Dirk McCormick | last post: by
3 posts views Thread by rene | last post: by
reply views Thread by George Durzi | last post: by
5 posts views Thread by nutsmuggler | last post: by
2 posts views Thread by Andrus | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.