473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 3939
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

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

Similar topics

5
by: Marcel | last post by:
Hello all, I am working on a generic (php) script to produce (X)HTML Forms through XML and XSL based on field-definitions stored in a database. The basic way i did HTML-forms in PHP was like...
1
by: Spam sucks | last post by:
hello, i create a logging xml file with dom that could have an unknown count of results now it is 0 to 7 but it could be i have 14 or 50 results how can you read this out with xsl, with php you...
3
by: Tjerk Wolterink | last post by:
Hello, I want to create m elements in this form: <element index="n"/> Where n = 1 2 .. m Can is do this with an xsl:for loop?? And if so how
3
by: Tjerk Wolterink | last post by:
I posted my problem earlier, but i simplified the examples, and i know what the cause of the problem is, but i dont know the solution, my xml file: <?xml version="1.0" encoding="ISO-8859-1"?>...
2
by: Dirk McCormick | last post by:
I need to transform something that looks like this <!-- %%%%%%%%%%%%%%%%%%%%%%% --> <MyStructure> <Packages> <Package> <PkgBenefitCode>1</PkgBenefitCode> <PkgBenefitCode>2</PkgBenefitCode>...
3
by: rene | last post by:
Hello everybody, i have these 2 xml files ------1.xml------ <?xml version="1.0" encoding="UTF-8"?> <root> <element>1</element> <element>2</element> </root>
0
by: George Durzi | last post by:
Bear with me on this one because the explanation might be a bit lengthy. I have a stored procedure which is returning three tables (using three SELECTs). Table 0 is a list of all bills for a job...
5
by: nutsmuggler | last post by:
Hi. I am using xml to store my annotations. here is my document (a part of it): <?xml version="1.0" encoding="UTF-8"?> <text title="Finnegan's wake"> <book number="1"> <chapter number="5">...
2
by: Andrus | last post by:
xml data <?xml version="1.0" ?> <statement> <accounts> <account number="22"> <currency symbol="USD"> <transactions> <transaction> <id>1</id>
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.