XML/Output: Difference between revisions
Content added Content deleted
m (→{{header|Joy}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 14: | Line 14: | ||
Short & shrift</pre> |
Short & shrift</pre> |
||
Should produce the XML (but not necessarily with the indentation): |
Should produce the XML (but not necessarily with the indentation): |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
The document may include an <tt><?xml?></tt> declaration and document type declaration, but these are optional. If attempting this task by direct string manipulation, the implementation ''must'' include code to perform entity substitution for the characters that have entities defined in the XML 1.0 specification. |
The document may include an <tt><?xml?></tt> declaration and document type declaration, but these are optional. If attempting this task by direct string manipulation, the implementation ''must'' include code to perform entity substitution for the characters that have entities defined in the XML 1.0 specification. |
||
Line 26: | Line 26: | ||
'''Note to editors:''' Program output with escaped characters will be viewed as the character on the page so you need to 'escape-the-escapes' to make the RC entry display what would be shown in a plain text viewer (See [[Talk:XML_Creation#Escaping_Escapes|this]]). |
'''Note to editors:''' Program output with escaped characters will be viewed as the character on the page so you need to 'escape-the-escapes' to make the RC entry display what would be shown in a plain text viewer (See [[Talk:XML_Creation#Escaping_Escapes|this]]). |
||
Alternately, output can be placed in <nowiki><lang |
Alternately, output can be placed in <nowiki><syntaxhighlight lang="xml"></syntaxhighlight></nowiki> tags without any special treatment. |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program outputXml64.s */ |
/* program outputXml64.s */ |
||
Line 197: | Line 197: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">DEFINE PTR="CARD" |
||
DEFINE CHARACTER_SIZE="4" |
DEFINE CHARACTER_SIZE="4" |
||
TYPE Character=[PTR name,remark] |
TYPE Character=[PTR name,remark] |
||
Line 276: | Line 276: | ||
OutputCharacters(d,count) |
OutputCharacters(d,count) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/XML_Output.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/XML_Output.png Screenshot from Atari 8-bit computer] |
||
Line 293: | Line 293: | ||
character_remarks.adb: |
character_remarks.adb: |
||
< |
<syntaxhighlight lang="ada">with Ada.Strings.Unbounded; |
||
with Ada.Text_IO.Text_Streams; |
with Ada.Text_IO.Text_Streams; |
||
with DOM.Core.Documents; |
with DOM.Core.Documents; |
||
Line 332: | Line 332: | ||
N => My_Document, |
N => My_Document, |
||
Pretty_Print => True); |
Pretty_Print => True); |
||
end Character_Remarks;</ |
end Character_Remarks;</syntaxhighlight> |
||
=== Alternative version using Matreshka === |
=== Alternative version using Matreshka === |
||
Line 338: | Line 338: | ||
Uses [http://forge.ada-ru.org/matreshka Matreshka SAX API for XML]. |
Uses [http://forge.ada-ru.org/matreshka Matreshka SAX API for XML]. |
||
< |
<syntaxhighlight lang="ada">with Ada.Wide_Wide_Text_IO; |
||
with League.Strings; |
with League.Strings; |
||
Line 390: | Line 390: | ||
(+"Tam O'Shanter", +"Burns: ""When chapman billies leave the street ..."""), |
(+"Tam O'Shanter", +"Burns: ""When chapman billies leave the street ..."""), |
||
(+"Emily", +"Short & shrift"))); |
(+"Emily", +"Short & shrift"))); |
||
end Main;</ |
end Main;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68"># returns a translation of str suitable for attribute values and content in an XML document # |
||
OP TOXMLSTRING = ( STRING str )STRING: |
OP TOXMLSTRING = ( STRING str )STRING: |
||
BEGIN |
BEGIN |
||
Line 439: | Line 439: | ||
) |
) |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks><Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ...</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ...</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program outputXml.s */ |
/* program outputXml.s */ |
||
Line 650: | Line 650: | ||
bx lr @ return |
bx lr @ return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 662: | Line 662: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">gosub constants |
||
names := xmlescape(names) |
names := xmlescape(names) |
||
remarks := xmlescape(remarks) |
remarks := xmlescape(remarks) |
||
Line 711: | Line 711: | ||
Short & shrift |
Short & shrift |
||
) |
) |
||
return</ |
return</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="applesoftbasic">100 Q$ = CHR$(34) |
||
110 DE$(0) = "April" |
110 DE$(0) = "April" |
||
120 RE$(0) = "Bubbly: I'm > Tam and <= Emily" |
120 RE$(0) = "Bubbly: I'm > Tam and <= Emily" |
||
Line 744: | Line 744: | ||
370 PRINT C$; |
370 PRINT C$; |
||
380 NEXT N |
380 NEXT N |
||
390 RETURN</ |
390 RETURN</syntaxhighlight> |
||
Line 751: | Line 751: | ||
Not terribly different from the Applesoft BASIC example above, but this allows for a few more character/entity translations. |
Not terribly different from the Applesoft BASIC example above, but this allows for a few more character/entity translations. |
||
< |
<syntaxhighlight lang="gwbasic">1 rem xml/output - commodore basic |
||
2 rem rosetta code |
2 rem rosetta code |
||
5 print chr$(147);chr$(14):gosub 900 |
5 print chr$(147);chr$(14):gosub 900 |
||
Line 805: | Line 805: | ||
1010 data "Emily","Short & shrift" |
1010 data "Emily","Short & shrift" |
||
1015 data "Joey","Always look ^." |
1015 data "Joey","Always look ^." |
||
1999 data "[end]","[end]"</ |
1999 data "[end]","[end]"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 834: | Line 834: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( ( 2XML |
||
= PCDATAentities attributeValueEntities doAll doAttributes |
= PCDATAentities attributeValueEntities doAll doAttributes |
||
, xml |
, xml |
||
Line 945: | Line 945: | ||
) |
) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre><?xml?> |
<pre><?xml?> |
||
Line 955: | Line 955: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{libheader|libXML}} |
{{libheader|libXML}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 994: | Line 994: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
{{works with|C sharp|C#|3.0+}} |
{{works with|C sharp|C#|3.0+}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 1,024: | Line 1,024: | ||
Console.WriteLine(xml); |
Console.WriteLine(xml); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Library: Boost |
Library: Boost |
||
< |
<syntaxhighlight lang="cpp">#include <vector> |
||
#include <utility> |
#include <utility> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,073: | Line 1,073: | ||
xmlstring.append( "</CharacterRemarks>" ) ; |
xmlstring.append( "</CharacterRemarks>" ) ; |
||
return xmlstring ; |
return xmlstring ; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="lisp">(use 'clojure.xml) |
||
(defn character-remarks-xml [characters remarks] |
(defn character-remarks-xml [characters remarks] |
||
(with-out-str (emit-element |
(with-out-str (emit-element |
||
Line 1,084: | Line 1,084: | ||
{:tag :Character, |
{:tag :Character, |
||
:attrs {:name (item 0)}, |
:attrs {:name (item 0)}, |
||
:content [(item 1)]}) )})))</ |
:content [(item 1)]}) )})))</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 1,090: | Line 1,090: | ||
{{libheader|Closure XML}} |
{{libheader|Closure XML}} |
||
< |
<syntaxhighlight lang="lisp">(defun write-xml (characters lines &optional (out *standard-output*)) |
||
(let* ((doc (dom:create-document 'rune-dom:implementation nil nil nil)) |
(let* ((doc (dom:create-document 'rune-dom:implementation nil nil nil)) |
||
(chars (dom:append-child doc (dom:create-element doc "Characters")))) |
(chars (dom:append-child doc (dom:create-element doc "Characters")))) |
||
Line 1,099: | Line 1,099: | ||
(dom:append-child chars c))) |
(dom:append-child chars c))) |
||
characters lines) |
characters lines) |
||
(write-string (dom:map-document (cxml:make-rod-sink) doc) out)))</ |
(write-string (dom:map-document (cxml:make-rod-sink) doc) out)))</syntaxhighlight> |
||
Example of use: |
Example of use: |
||
< |
<syntaxhighlight lang="lisp">(write-xml '("April" "Tam O'Shanter" "Emily") |
||
'("Bubbly: I'm > Tam and <= Emily" |
'("Bubbly: I'm > Tam and <= Emily" |
||
"Burns: \"When chapman billies leave the street ...\"" |
"Burns: \"When chapman billies leave the street ...\"" |
||
"Short & shrift"))</ |
"Short & shrift"))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,115: | Line 1,115: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{libheader|KXML}} |
{{libheader|KXML}} |
||
< |
<syntaxhighlight lang="d">import kxml.xml; |
||
char[][][]characters = |
char[][][]characters = |
||
[["April","Bubbly: I'm > Tam and <= Emily"], |
[["April","Bubbly: I'm > Tam and <= Emily"], |
||
Line 1,132: | Line 1,132: | ||
addChars(root,characters); |
addChars(root,characters); |
||
std.stdio.writefln("%s",root.write); |
std.stdio.writefln("%s",root.write); |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
<?xml?> |
<?xml?> |
||
Line 1,149: | Line 1,149: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
//You need to use these units |
//You need to use these units |
||
uses |
uses |
||
Line 1,226: | Line 1,226: | ||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( xml_output ). |
-module( xml_output ). |
||
Line 1,242: | Line 1,242: | ||
contents() -> [{"April", "Bubbly: I'm > Tam and <= Emily"}, {"Tam O'Shanter", "Burns: \"When chapman billies leave the street ...\""}, {"Emily", "Short & shrift"}]. |
contents() -> [{"April", "Bubbly: I'm > Tam and <= Emily"}, {"Tam O'Shanter", "Burns: \"When chapman billies leave the street ...\""}, {"Emily", "Short & shrift"}]. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,251: | Line 1,251: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
{{trans|BASIC}} |
{{trans|BASIC}} |
||
< |
<syntaxhighlight lang="euphoria">function xmlquote(sequence s) |
||
sequence r |
sequence r |
||
r = "" |
r = "" |
||
Line 1,284: | Line 1,284: | ||
puts(1,"</Character>\n") |
puts(1,"</Character>\n") |
||
end for |
end for |
||
puts(1,"</CharacterRemarks>\n")</ |
puts(1,"</CharacterRemarks>\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,295: | Line 1,295: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">#light |
||
open System.Xml |
open System.Xml |
||
Line 1,316: | Line 1,316: | ||
root.AppendChild node |> ignore |
root.AppendChild node |> ignore |
||
) characters |
) characters |
||
doc.OuterXml</ |
doc.OuterXml</syntaxhighlight> |
||
<pre><CharacterRemarks> |
<pre><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm &gt; Tam and &lt;= Emily</Character> |
<Character name="April">Bubbly: I'm &gt; Tam and &lt;= Emily</Character> |
||
Line 1,324: | Line 1,324: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: sequences xml.syntax xml.writer ; |
||
: print-character-remarks ( names remarks -- ) |
: print-character-remarks ( names remarks -- ) |
||
[ [XML <Character name=<-> ><-></Character> XML] ] 2map |
[ [XML <Character name=<-> ><-></Character> XML] ] 2map |
||
[XML <CharacterRemarks><-></CharacterRemarks> XML] pprint-xml ;</ |
[XML <CharacterRemarks><-></CharacterRemarks> XML] pprint-xml ;</syntaxhighlight> |
||
Example of usage: |
Example of usage: |
||
< |
<syntaxhighlight lang="factor">{ "April" "Tam O'Shanter" "Emily" } { |
||
"Bubbly: I'm > Tam and <= Emily" |
"Bubbly: I'm > Tam and <= Emily" |
||
"Burns: \"When chapman billies leave the street ...\"" |
"Burns: \"When chapman billies leave the street ...\"" |
||
"Short & shrift" |
"Short & shrift" |
||
} print-remarks</ |
} print-remarks</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
using xml |
using xml |
||
Line 1,364: | Line 1,364: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} (not exactly conforming): |
{{out}} (not exactly conforming): |
||
Line 1,380: | Line 1,380: | ||
{{libheader|Forth Foundation Library}} |
{{libheader|Forth Foundation Library}} |
||
< |
<syntaxhighlight lang="forth">include ffl/est.fs |
||
include ffl/xos.fs |
include ffl/xos.fs |
||
Line 1,420: | Line 1,420: | ||
\ Output xml string |
\ Output xml string |
||
xml str-get type cr</ |
xml str-get type cr</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="basic">Data "April", "Bubbly: I'm > Tam and <= Emily", _ |
||
"Tam O'Shanter", "Burns: ""When chapman billies leave the street ...""", _ |
"Tam O'Shanter", "Burns: ""When chapman billies leave the street ...""", _ |
||
"Emily", "Short & shrift" |
"Emily", "Short & shrift" |
||
Line 1,462: | Line 1,462: | ||
Next |
Next |
||
Function = r |
Function = r |
||
End Function</ |
End Function</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
'''Using package xml to marshal from a data structure:''' |
'''Using package xml to marshal from a data structure:''' |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,500: | Line 1,500: | ||
} |
} |
||
fmt.Println(x) |
fmt.Println(x) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
Using the text/template package to generate text: |
Using the text/template package to generate text: |
||
(but still leaning on the xml package for escaping.) |
(but still leaning on the xml package for escaping.) |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,555: | Line 1,555: | ||
} |
} |
||
fmt.Println(x) |
fmt.Println(x) |
||
}</ |
}</syntaxhighlight> |
||
Output is same as marshalled version. |
Output is same as marshalled version. |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">def writer = new StringWriter() |
||
def builder = new groovy.xml.MarkupBuilder(writer) |
def builder = new groovy.xml.MarkupBuilder(writer) |
||
def names = ["April", "Tam O'Shanter", "Emily"] |
def names = ["April", "Tam O'Shanter", "Emily"] |
||
Line 1,568: | Line 1,568: | ||
} |
} |
||
println writer.toString()</ |
println writer.toString()</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
This implementation uses the [http://hackage.haskell.org/package/xml <code>xml</code> package]. |
This implementation uses the [http://hackage.haskell.org/package/xml <code>xml</code> package]. |
||
< |
<syntaxhighlight lang="haskell">import Text.XML.Light |
||
characterRemarks :: [String] -> [String] -> String |
characterRemarks :: [String] -> [String] -> String |
||
Line 1,585: | Line 1,585: | ||
[Attr (unqual "name") name] |
[Attr (unqual "name") name] |
||
[Text $ CData CDataText remark Nothing] |
[Text $ CData CDataText remark Nothing] |
||
Nothing</ |
Nothing</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">CHARACTER names="April~Tam O'Shanter~Emily~" |
||
CHARACTER remarks*200/%Bubbly: I'm > Tam and <= Emily~Burns: "When chapman billies leave the street ..."~Short & shrift~%/ |
CHARACTER remarks*200/%Bubbly: I'm > Tam and <= Emily~Burns: "When chapman billies leave the street ..."~Short & shrift~%/ |
||
CHARACTER XML*1000 |
CHARACTER XML*1000 |
||
Line 1,602: | Line 1,602: | ||
XML = TRIM(XML) // '<Character name="' // name // '">' // remark // '</Character>' // $CRLF |
XML = TRIM(XML) // '<Character name="' // name // '">' // remark // '</Character>' // $CRLF |
||
ENDDO |
ENDDO |
||
XML = TRIM(XML) // "</CharacterRemarks>"</ |
XML = TRIM(XML) // "</CharacterRemarks>"</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 1,608: | Line 1,608: | ||
First create the table of substitutions and the verb which properly escapes the input string: |
First create the table of substitutions and the verb which properly escapes the input string: |
||
< |
<syntaxhighlight lang="j">tbl=: ('"e;'; '&'; '<'; '>') (a.i.'"&<>')} <"0 a. |
||
esc=: [:; {&tbl@:i.~&a.</ |
esc=: [:; {&tbl@:i.~&a.</syntaxhighlight> |
||
Then create the verb which combines name with remark: |
Then create the verb which combines name with remark: |
||
< |
<syntaxhighlight lang="j">cmb=: [:; dyad define &.> |
||
'<Character name="', (esc x), '">', (esc y), '</Character>', LF |
'<Character name="', (esc x), '">', (esc y), '</Character>', LF |
||
)</ |
)</syntaxhighlight> |
||
Finally, create the verb which creates the final XML: |
Finally, create the verb which creates the final XML: |
||
< |
<syntaxhighlight lang="j">xmlify=: '<CharacterRemarks>', LF, cmb, '</CharacterRemarks>'"_</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="j">names=: 'April'; 'Tam O''Shanter'; 'Emily' |
||
remarks=: <;._2]0 :0 |
remarks=: <;._2]0 :0 |
||
Line 1,627: | Line 1,627: | ||
Burns: "When chapman billies leave the street ..." |
Burns: "When chapman billies leave the street ..." |
||
Short & shrift |
Short & shrift |
||
)</ |
)</syntaxhighlight> |
||
<lang> names xmlify remarks |
<syntaxhighlight lang="text"> names xmlify remarks |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
<Character name="April"> I'm > Tam and <= Emily</Character> |
<Character name="April"> I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter"> Burns: "e;When chapman billies leave the street ..."e;</Character> |
<Character name="Tam O'Shanter"> Burns: "e;When chapman billies leave the street ..."e;</Character> |
||
<Character name="Emily"> Short & shrift</Character> |
<Character name="Emily"> Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
===Using DOM=== |
===Using DOM=== |
||
< |
<syntaxhighlight lang="java">import java.io.StringWriter; |
||
import javax.xml.parsers.DocumentBuilderFactory; |
import javax.xml.parsers.DocumentBuilderFactory; |
||
Line 1,698: | Line 1,698: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Result: |
Result: |
||
< |
<syntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character></ |
<Character name="Emily">Short & shrift</Character></syntaxhighlight> |
||
===Using the Streaming API for XML (StAX)=== |
===Using the Streaming API for XML (StAX)=== |
||
< |
<syntaxhighlight lang="java">import java.io.StringWriter; |
||
import javax.xml.stream.XMLOutputFactory; |
import javax.xml.stream.XMLOutputFactory; |
||
Line 1,745: | Line 1,745: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
This produces: |
This produces: |
||
< |
<syntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8"?><CharacterRemarks><Character name="April">Bubbly: I'm > Tam and <= Emily</Character><Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character><Character name="Emily">Short & shrift</Character></CharacterRemarks></syntaxhighlight> |
||
=={{header|Joy}}== |
=={{header|Joy}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="joy"> |
||
DEFINE subst == |
DEFINE subst == |
||
[[['< "<" putchars] |
[[['< "<" putchars] |
||
Line 1,770: | Line 1,770: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using LightXML |
||
dialog = [("April", "Bubbly: I'm > Tam and <= Emily"), |
dialog = [("April", "Bubbly: I'm > Tam and <= Emily"), |
||
Line 1,786: | Line 1,786: | ||
println(xdoc) |
println(xdoc) |
||
</ |
</syntaxhighlight>{{output}}<pre> |
||
<?xml version="1.0" encoding="utf-8"?> |
<?xml version="1.0" encoding="utf-8"?> |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
Line 1,796: | Line 1,796: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.3 |
||
import javax.xml.parsers.DocumentBuilderFactory |
import javax.xml.parsers.DocumentBuilderFactory |
||
Line 1,840: | Line 1,840: | ||
println(sw) |
println(sw) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
Lasso has built in support for both creating and parsing xml. |
Lasso has built in support for both creating and parsing xml. |
||
< |
<syntaxhighlight lang="lasso">define character2xml(names::array, remarks::array) => { |
||
fail_if(#names -> size != #remarks -> size, -1, 'Input arrays not of same size') |
fail_if(#names -> size != #remarks -> size, -1, 'Input arrays not of same size') |
||
Line 1,887: | Line 1,887: | ||
array(`April`, `Tam O'Shanter`, `Emily`), |
array(`April`, `Tam O'Shanter`, `Emily`), |
||
array(`Bubbly: I'm > Tam and <= Emily`, `Burns: "When chapman billies leave the street ..."`, `Short & shrift`) |
array(`Bubbly: I'm > Tam and <= Emily`, `Burns: "When chapman billies leave the street ..."`, `Short & shrift`) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="xml"> |
||
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
||
<!DOCTYPE svg:svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> |
<!DOCTYPE svg:svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> |
||
Line 1,898: | Line 1,898: | ||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Using the LuaXML library as available via luarocks. Note that strings in Lua can be enclosed in either single or double quotes to help reduce the need for escape characters. |
Using the LuaXML library as available via luarocks. Note that strings in Lua can be enclosed in either single or double quotes to help reduce the need for escape characters. |
||
< |
<syntaxhighlight lang="lua">require("LuaXML") |
||
function addNode(parent, nodeName, key, value, content) |
function addNode(parent, nodeName, key, value, content) |
||
Line 1,914: | Line 1,914: | ||
addNode(root, "Character", "name", "Tam O'Shanter", 'Burns: "When chapman billies leave the street ..."') |
addNode(root, "Character", "name", "Tam O'Shanter", 'Burns: "When chapman billies leave the street ..."') |
||
addNode(root, "Character", "name", "Emily", "Short & shrift") |
addNode(root, "Character", "name", "Emily", "Short & shrift") |
||
print(root)</ |
print(root)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
Note also that LuaXML escapes quote marks and apostrophes, which makes the output slightly different to the task requirement. This can be 'fixed' if necessary using Lua's in-built string.gsub function: |
Note also that LuaXML escapes quote marks and apostrophes, which makes the output slightly different to the task requirement. This can be 'fixed' if necessary using Lua's in-built string.gsub function: |
||
< |
<syntaxhighlight lang="lua">xmlStr = xml.str(root):gsub("'", "'"):gsub(""", '"') |
||
print(xmlStr)</ |
print(xmlStr)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
Some tricky input with the second remark |
Some tricky input with the second remark |
||
< |
<syntaxhighlight lang="mathematica">c = {"April", "Tam O'Shanter","Emily"}; |
||
r = {"Bubbly:I'm > Tam and <= Emily" , |
r = {"Bubbly:I'm > Tam and <= Emily" , |
||
StringReplace["Burns:\"When chapman billies leave the street ...\"", "\"" -> ""], "Short & shrift"}; |
StringReplace["Burns:\"When chapman billies leave the street ...\"", "\"" -> ""], "Short & shrift"}; |
||
Line 1,940: | Line 1,940: | ||
XMLElement["Character", {"name" -> c[[2]]}, {r[[2]]}], |
XMLElement["Character", {"name" -> c[[2]]}, {r[[2]]}], |
||
XMLElement["Character", {"name" -> c[[3]]}, {r[[3]]}] |
XMLElement["Character", {"name" -> c[[3]]}, {r[[3]]}] |
||
}], "XML", "AttributeQuoting" -> "\""]</ |
}], "XML", "AttributeQuoting" -> "\""]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre><CharacterRemarks> |
<pre><CharacterRemarks> |
||
Line 1,949: | Line 1,949: | ||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">RootXML = com.mathworks.xml.XMLUtils.createDocument('CharacterRemarks'); |
||
docRootNode = RootXML.getDocumentElement; |
docRootNode = RootXML.getDocumentElement; |
||
thisElement = RootXML.createElement('Character'); |
thisElement = RootXML.createElement('Character'); |
||
Line 1,962: | Line 1,962: | ||
thisElement.setAttribute('Name','Emily') |
thisElement.setAttribute('Name','Emily') |
||
thisElement.setTextContent('Short & shrift'); |
thisElement.setTextContent('Short & shrift'); |
||
docRootNode.appendChild(thisElement);</ |
docRootNode.appendChild(thisElement);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,982: | Line 1,982: | ||
===Using DOM=== |
===Using DOM=== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary |
options replace format comments java crossref savelog symbols nobinary |
||
Line 2,047: | Line 2,047: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Output |
;Output |
||
< |
<syntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
Line 2,055: | Line 2,055: | ||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Using the Streaming API for XML (StAX)=== |
===Using the Streaming API for XML (StAX)=== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary |
options replace format comments java crossref savelog symbols nobinary |
||
Line 2,107: | Line 2,107: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Output |
;Output |
||
< |
<syntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8"?> |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
Line 2,115: | Line 2,115: | ||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import xmltree, strtabs, sequtils |
||
proc charsToXML(names, remarks: seq[string]): XmlNode = |
proc charsToXML(names, remarks: seq[string]): XmlNode = |
||
Line 2,128: | Line 2,128: | ||
@["Bubbly: I'm > Tam and <= Emily", |
@["Bubbly: I'm > Tam and <= Emily", |
||
"Burns: \"When chapman billies leave the street ...\"", |
"Burns: \"When chapman billies leave the street ...\"", |
||
"Short & shrift"])</ |
"Short & shrift"])</syntaxhighlight> |
||
Output: |
Output: |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck">use Data.XML; |
||
use Collection.Generic; |
use Collection.Generic; |
||
Line 2,166: | Line 2,166: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 2,172: | Line 2,172: | ||
from the toplevel using the library [http://tech.motion-twin.com/xmllight.html xml-light]: |
from the toplevel using the library [http://tech.motion-twin.com/xmllight.html xml-light]: |
||
< |
<syntaxhighlight lang="ocaml"># #directory "+xml-light" (* or maybe "+site-lib/xml-light" *) ;; |
||
# #load "xml-light.cma" ;; |
# #load "xml-light.cma" ;; |
||
Line 2,194: | Line 2,194: | ||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
- : unit = ()</ |
- : unit = ()</syntaxhighlight> |
||
Another solution using the library [http://erratique.ch/software/xmlm xmlm]: |
Another solution using the library [http://erratique.ch/software/xmlm xmlm]: |
||
< |
<syntaxhighlight lang="ocaml">#directory "+xmlm" |
||
#load "xmlm.cmo" |
#load "xmlm.cmo" |
||
open Xmlm |
open Xmlm |
||
Line 2,219: | Line 2,219: | ||
) datas; |
) datas; |
||
output xo (`El_end); |
output xo (`El_end); |
||
print_newline()</ |
print_newline()</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
It is natural to represent XML document as nested Oz records. Writing a function that serializes records to textual XML is not too difficult. |
It is natural to represent XML document as nested Oz records. Writing a function that serializes records to textual XML is not too difficult. |
||
< |
<syntaxhighlight lang="oz">declare |
||
proc {Main} |
proc {Main} |
||
Names = ["April" |
Names = ["April" |
||
Line 2,312: | Line 2,312: | ||
end |
end |
||
in |
in |
||
{Main}</ |
{Main}</syntaxhighlight> |
||
Output: |
Output: |
||
< |
<syntaxhighlight lang="xml"><?xml version="1.0" ?> |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|XML::Mini}} |
{{libheader|XML::Mini}} |
||
< |
<syntaxhighlight lang="perl">#! /usr/bin/perl |
||
use strict; |
use strict; |
||
use XML::Mini::Document; |
use XML::Mini::Document; |
||
Line 2,342: | Line 2,342: | ||
$stud->text($s->[1]); |
$stud->text($s->[1]); |
||
} |
} |
||
print $doc->toString();</ |
print $doc->toString();</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">hchars</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hsubs</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"<"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;"</span><span style="color: #0000FF;">},</span> |
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">hchars</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hsubs</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"<"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;"</span><span style="color: #0000FF;">},</span> |
||
Line 2,380: | Line 2,380: | ||
-- <CharacterName="Emily">Short &amp; shrift</Character> |
-- <CharacterName="Emily">Short &amp; shrift</Character> |
||
-- </CharacterRemarks></span> |
-- </CharacterRemarks></span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/xml.l") |
||
(de characterRemarks (Names Remarks) |
(de characterRemarks (Names Remarks) |
||
Line 2,401: | Line 2,401: | ||
"I'm > Tam and <= Emily" |
"I'm > Tam and <= Emily" |
||
"Burns: \"When chapman billies leave the street ..." |
"Burns: \"When chapman billies leave the street ..." |
||
"Short & shrift" ) )</ |
"Short & shrift" ) )</syntaxhighlight> |
||
Output: |
Output: |
||
<pre><CharacterRemarks> |
<pre><CharacterRemarks> |
||
Line 2,410: | Line 2,410: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">DataSection |
||
dataItemCount: |
dataItemCount: |
||
Data.i 3 |
Data.i 3 |
||
Line 2,468: | Line 2,468: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre><CharacterRemarks> |
<pre><CharacterRemarks> |
||
Line 2,480: | Line 2,480: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
Normal output is all one line of XML, the .replace(...) makes it more readable. |
Normal output is all one line of XML, the .replace(...) makes it more readable. |
||
< |
<syntaxhighlight lang="python">>>> from xml.etree import ElementTree as ET |
||
>>> from itertools import izip |
>>> from itertools import izip |
||
>>> def characterstoxml(names, remarks): |
>>> def characterstoxml(names, remarks): |
||
Line 2,493: | Line 2,493: | ||
remarks = [ "Bubbly: I'm > Tam and <= Emily", |
remarks = [ "Bubbly: I'm > Tam and <= Emily", |
||
'Burns: "When chapman billies leave the street ..."', |
'Burns: "When chapman billies leave the street ..."', |
||
'Short & shrift' ] ).replace('><','>\n<')</ |
'Short & shrift' ] ).replace('><','>\n<')</syntaxhighlight> |
||
Gives the output: |
Gives the output: |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
{{libheader|XML}} |
{{libheader|XML}} |
||
< |
<syntaxhighlight lang="r">library(XML) |
||
char2xml <- function(names, remarks){ |
char2xml <- function(names, remarks){ |
||
tt <- xmlHashTree() |
tt <- xmlHashTree() |
||
Line 2,515: | Line 2,515: | ||
} |
} |
||
output <- char2xml( names=c("April","Tam O'Shanter","Emily"), |
output <- char2xml( names=c("April","Tam O'Shanter","Emily"), |
||
remarks=c("Bubbly: I'm > Tam and <= Emily", 'Burns: "When chapman billies leave the street ..."', "Short & shrift") )</ |
remarks=c("Bubbly: I'm > Tam and <= Emily", 'Burns: "When chapman billies leave the street ..."', "Short & shrift") )</syntaxhighlight> |
||
Gives the output: <!-- manually inserted "&" where required to protect actual R output from the browser --> |
Gives the output: <!-- manually inserted "&" where required to protect actual R output from the browser --> |
||
Line 2,526: | Line 2,526: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require xml) |
(require xml) |
||
Line 2,543: | Line 2,543: | ||
"Burns: \"When chapman billies leave the street ...\"" |
"Burns: \"When chapman billies leave the street ...\"" |
||
"Short & shrift")))) |
"Short & shrift")))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
< |
<syntaxhighlight lang="xml"> |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
<Character name="April"> |
<Character name="April"> |
||
Line 2,559: | Line 2,559: | ||
</Character> |
</Character> |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,565: | Line 2,565: | ||
{{works with|Rakudo|2018.05}} |
{{works with|Rakudo|2018.05}} |
||
<lang |
<syntaxhighlight lang="raku" line>use XML::Writer; |
||
my @students = |
my @students = |
||
Line 2,575: | Line 2,575: | ||
my @lines = map { :Character[:name(.[0]), .[1]] }, @students; |
my @lines = map { :Character[:name(.[0]), .[1]] }, @students; |
||
say XML::Writer.serialize( CharacterRemarks => @lines );</ |
say XML::Writer.serialize( CharacterRemarks => @lines );</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks><Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character></CharacterRemarks></ |
<Character name="Emily">Short & shrift</Character></CharacterRemarks></syntaxhighlight> |
||
=={{header|Rascal}}== |
=={{header|Rascal}}== |
||
< |
<syntaxhighlight lang="rascal">import Prelude; |
||
import lang::xml::DOM; |
import lang::xml::DOM; |
||
Line 2,597: | Line 2,597: | ||
return println(xmlPretty(x)); |
return println(xmlPretty(x)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
This gives an output: |
This gives an output: |
||
< |
<syntaxhighlight lang="rascal">rascal>xmloutput(charnames, remarks) |
||
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
Line 2,608: | Line 2,608: | ||
ok</ |
ok</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
REXX doesn't have any functions to handle XML entities, an abbreviated version is included here. |
REXX doesn't have any functions to handle XML entities, an abbreviated version is included here. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program creates an HTML (XML) list of character names and corresponding remarks. */ |
||
charname. = |
charname. = |
||
charname.1 = "April" |
charname.1 = "April" |
||
Line 2,728: | Line 2,728: | ||
xml_: parse arg _ /*make an XML entity (&xxxx;) */ |
xml_: parse arg _ /*make an XML entity (&xxxx;) */ |
||
if pos(_, $)\==0 then return changestr(_, $, "&"arg(2)";") |
if pos(_, $)\==0 then return changestr(_, $, "&"arg(2)";") |
||
return $</ |
return $</syntaxhighlight> |
||
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]]. |
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]]. |
||
Line 2,750: | Line 2,750: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
The {{libheader|REXML}}library handles character mapping when adding attributes and text. |
The {{libheader|REXML}}library handles character mapping when adding attributes and text. |
||
< |
<syntaxhighlight lang="ruby">require 'rexml/document' |
||
include REXML |
include REXML |
||
Line 2,767: | Line 2,767: | ||
# output with indentation |
# output with indentation |
||
doc.write($stdout, 2)</ |
doc.write($stdout, 2)</syntaxhighlight> |
||
produces <!-- manually inserted "&" where required to protect actual Ruby output from the browser --> |
produces <!-- manually inserted "&" where required to protect actual Ruby output from the browser --> |
||
Line 2,783: | Line 2,783: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">extern crate xml; |
||
use std::collections::HashMap; |
use std::collections::HashMap; |
||
Line 2,840: | Line 2,840: | ||
assert!(output.contains("<Character name=\"Emily\">Short & shrift</Character>")); |
assert!(output.contains("<Character name=\"Emily\">Short & shrift</Character>")); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">val names = List("April", "Tam O'Shanter", "Emily") |
||
val remarks = List("Bubbly: I'm > Tam and <= Emily", """Burns: "When chapman billies leave the street ..."""", "Short & shrift") |
val remarks = List("Bubbly: I'm > Tam and <= Emily", """Burns: "When chapman billies leave the street ..."""", "Short & shrift") |
||
Line 2,852: | Line 2,852: | ||
characterRemarks(names, remarks) |
characterRemarks(names, remarks) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Result: |
Result: |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character><Character name="Tam O'Shanter">Burns: |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character><Character name="Tam O'Shanter">Burns: |
||
"When chapman billies leave the street ..."</Character><Character name="Emily">Short & shrift</Character> |
"When chapman billies leave the street ..."</Character><Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
< |
<syntaxhighlight lang="sensetalk">put ("April", "Tam O'Shanter", "Emily") into names |
||
put ("Bubbly: I'm > Tam and <= Emily", <<Burns: "When chapman billies leave the street ...">>, "Short & shrift") into remarks |
put ("Bubbly: I'm > Tam and <= Emily", <<Burns: "When chapman billies leave the street ...">>, "Short & shrift") into remarks |
||
put (_tag: "CharacterRemarks") as tree into document |
put (_tag: "CharacterRemarks") as tree into document |
||
Line 2,867: | Line 2,867: | ||
insert (_tag: "Character", name: name, _children: item the counter of remarks) as tree into document's _children |
insert (_tag: "Character", name: name, _children: item the counter of remarks) as tree into document's _children |
||
end repeat |
end repeat |
||
put document</ |
put document</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,880: | Line 2,880: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">require('XML::Mini::Document'); |
||
var students = [ |
var students = [ |
||
Line 2,898: | Line 2,898: | ||
}; |
}; |
||
print doc.toString;</ |
print doc.toString;</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">lobby define: #remarks -> ( |
||
{'April' -> 'Bubbly: I\'m > Tam and <= Emily'. |
{'April' -> 'Bubbly: I\'m > Tam and <= Emily'. |
||
'Tam O\'Shanter' -> 'Burns: "When chapman billies leave the street ..."'. |
'Tam O\'Shanter' -> 'Burns: "When chapman billies leave the street ..."'. |
||
Line 2,915: | Line 2,915: | ||
]. |
]. |
||
inform: writer contents</ |
inform: writer contents</syntaxhighlight> |
||
Produces: |
Produces: |
||
<div style="width:full;overflow:scroll"> |
<div style="width:full;overflow:scroll"> |
||
Line 2,924: | Line 2,924: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
===Using only Tcl string manipulation=== |
===Using only Tcl string manipulation=== |
||
< |
<syntaxhighlight lang="tcl">proc xquote string { |
||
list [string map "' ' \\\" " < > > < & &" $string] |
list [string map "' ' \\\" " < > > < & &" $string] |
||
} |
} |
||
Line 2,951: | Line 2,951: | ||
append cmd " \[< Character {Name [xquote $name]} [xquote $comment]\]" |
append cmd " \[< Character {Name [xquote $name]} [xquote $comment]\]" |
||
} |
} |
||
puts [eval $cmd]</ |
puts [eval $cmd]</syntaxhighlight> |
||
produces<pre><CharacterRemarks><Character Name='April'>Bubbly: I&apos;m &lt; Tam and &gt;= Emily</Character> |
produces<pre><CharacterRemarks><Character Name='April'>Bubbly: I&apos;m &lt; Tam and &gt;= Emily</Character> |
||
<Character Name='Tam O&apos;Shanter'>Burns: &quot;When chapman billies leave the street ...&quot;</Character> |
<Character Name='Tam O&apos;Shanter'>Burns: &quot;When chapman billies leave the street ...&quot;</Character> |
||
Line 2,958: | Line 2,958: | ||
===Working with DOM trees=== |
===Working with DOM trees=== |
||
Using {{libheader|tDOM}} |
Using {{libheader|tDOM}} |
||
< |
<syntaxhighlight lang="tcl">package require tdom |
||
set xml [dom createDocument CharacterRemarks] |
set xml [dom createDocument CharacterRemarks] |
||
foreach {name comment} { |
foreach {name comment} { |
||
Line 2,970: | Line 2,970: | ||
[$xml documentElement] appendChild $elem |
[$xml documentElement] appendChild $elem |
||
} |
} |
||
$xml asXML</ |
$xml asXML</syntaxhighlight> |
||
<pre><CharacterRemarks> |
<pre><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm &amp;gt; Tam and &amp;lt;= Emily</Character> |
<Character name="April">Bubbly: I'm &amp;gt; Tam and &amp;lt;= Emily</Character> |
||
Line 2,977: | Line 2,977: | ||
</CharacterRemarks></pre> |
</CharacterRemarks></pre> |
||
Using {{libheader|TclXML}} |
Using {{libheader|TclXML}} |
||
< |
<syntaxhighlight lang="tcl">package require dom |
||
set xml [dom::DOMImplementation create] |
set xml [dom::DOMImplementation create] |
||
set root [dom::document createElement $xml CharacterRemarks] |
set root [dom::document createElement $xml CharacterRemarks] |
||
Line 2,989: | Line 2,989: | ||
dom::document createTextNode $element $comment |
dom::document createTextNode $element $comment |
||
} |
} |
||
dom::DOMImplementation serialize $xml -indent 1</ |
dom::DOMImplementation serialize $xml -indent 1</syntaxhighlight> |
||
produces (with line breaks added for clarity: |
produces (with line breaks added for clarity: |
||
<pre><?xml version="1.0"?> |
<pre><?xml version="1.0"?> |
||
Line 2,999: | Line 2,999: | ||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
STRUCTURE xmloutput |
STRUCTURE xmloutput |
||
Line 3,021: | Line 3,021: | ||
WRITE/NEXT d |
WRITE/NEXT d |
||
ENDACCESS d |
ENDACCESS d |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output in file "dest": |
Output in file "dest": |
||
<pre> |
<pre> |
||
Line 3,033: | Line 3,033: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
Saves in a file. |
Saves in a file. |
||
<syntaxhighlight lang="vbscript}}"> |
|||
<lang VBScript}}> |
|||
Set objXMLDoc = CreateObject("msxml2.domdocument") |
Set objXMLDoc = CreateObject("msxml2.domdocument") |
||
Line 3,051: | Line 3,051: | ||
objRoot.appendChild objNode |
objRoot.appendChild objNode |
||
End Function |
End Function |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
The input data is given in an edit buffer, one name+remark pair on each line line, separated with TAB character. |
The input data is given in an edit buffer, one name+remark pair on each line line, separated with TAB character. |
||
< |
<syntaxhighlight lang="vedit">// Replace special characters with entities: |
||
Replace("&", "&", BEGIN+ALL+NOERR) // this must be the first replace! |
Replace("&", "&", BEGIN+ALL+NOERR) // this must be the first replace! |
||
Replace("<", "<", BEGIN+ALL+NOERR) |
Replace("<", "<", BEGIN+ALL+NOERR) |
||
Line 3,072: | Line 3,072: | ||
} |
} |
||
EOF |
EOF |
||
IT("</CharacterRemarks>") IN</ |
IT("</CharacterRemarks>") IN</syntaxhighlight> |
||
Example input: |
Example input: |
||
Line 3,091: | Line 3,091: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
< |
<syntaxhighlight lang="vbnet">Module XMLOutput |
||
Sub Main() |
Sub Main() |
||
Dim charRemarks As New Dictionary(Of String, String) |
Dim charRemarks As New Dictionary(Of String, String) |
||
Line 3,104: | Line 3,104: | ||
Console.WriteLine(xml) |
Console.WriteLine(xml) |
||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
Output: |
Output: |
||
Line 3,116: | Line 3,116: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">var escapes = [ |
||
["&" , "&"], // must do this one first |
["&" , "&"], // must do this one first |
||
["\"", """], |
["\"", """], |
||
Line 3,146: | Line 3,146: | ||
"Short & shrift" |
"Short & shrift" |
||
] |
] |
||
xmlDoc.call(names, remarks)</ |
xmlDoc.call(names, remarks)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="xml"><CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks></ |
</CharacterRemarks></syntaxhighlight> |
||
<br> |
<br> |
||
{{libheader|Wren-xsequence}} |
{{libheader|Wren-xsequence}} |
||
Alternatively, using an XML library. |
Alternatively, using an XML library. |
||
< |
<syntaxhighlight lang="ecmascript">import "./xsequence" for XDocument, XElement, XAttribute |
||
var createXmlDoc = Fn.new { |names, remarks| |
var createXmlDoc = Fn.new { |names, remarks| |
||
Line 3,176: | Line 3,176: | ||
] |
] |
||
var doc = createXmlDoc.call(names, remarks) |
var doc = createXmlDoc.call(names, remarks) |
||
System.print(doc)</ |
System.print(doc)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Same as the first version but prefixed by: |
Same as the first version but prefixed by: |
||
< |
<syntaxhighlight lang="xml"><?xml version="1.0" encoding="utf-8"?></syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code ChOut=8, CrLf=9, Text=12; |
||
string 0; \use zero-terminated strings |
string 0; \use zero-terminated strings |
||
Line 3,211: | Line 3,211: | ||
]; |
]; |
||
Text(0, "</CharacterRemarks>"); CrLf(0); |
Text(0, "</CharacterRemarks>"); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,232: | Line 3,232: | ||
Variant #1-1: using a FLOWR expression and element constructors |
Variant #1-1: using a FLOWR expression and element constructors |
||
< |
<syntaxhighlight lang="xquery"> |
||
let $names := ("April","Tam O'Shanter","Emily") |
let $names := ("April","Tam O'Shanter","Emily") |
||
let $remarks := ("Bubbly: I'm > Tam and <= Emily", 'Burns: "When chapman billies leave the street ..."',"Short & shrift") |
let $remarks := ("Bubbly: I'm > Tam and <= Emily", 'Burns: "When chapman billies leave the street ..."',"Short & shrift") |
||
Line 3,242: | Line 3,242: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Variant #1-2: using a FLOWR expression and literal XML |
Variant #1-2: using a FLOWR expression and literal XML |
||
< |
<syntaxhighlight lang="xquery"> |
||
let $names := ("April","Tam O'Shanter","Emily") |
let $names := ("April","Tam O'Shanter","Emily") |
||
let $remarks := ("Bubbly: I'm > Tam and <= Emily", 'Burns: "When chapman billies leave the street ..."',"Short & shrift") |
let $remarks := ("Bubbly: I'm > Tam and <= Emily", 'Burns: "When chapman billies leave the street ..."',"Short & shrift") |
||
Line 3,255: | Line 3,255: | ||
} |
} |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
</syntaxhighlight> |
|||
</lang> |
|||
Variant #2-1: using a Higher Order Function and element constructors |
Variant #2-1: using a Higher Order Function and element constructors |
||
< |
<syntaxhighlight lang="xquery"> |
||
xquery version "3.1"; |
xquery version "3.1"; |
||
Line 3,272: | Line 3,272: | ||
}) |
}) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Variant #2-2: using a Higher Order Function and literal XML expressions :) |
Variant #2-2: using a Higher Order Function and literal XML expressions :) |
||
< |
<syntaxhighlight lang="xquery"> |
||
xquery version "3.1"; |
xquery version "3.1"; |
||
Line 3,288: | Line 3,288: | ||
} |
} |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
</syntaxhighlight> |
|||
</lang> |
|||
Result for all variants: |
Result for all variants: |
||
< |
<syntaxhighlight lang="xml"> |
||
<CharacterRemarks> |
<CharacterRemarks> |
||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
||
Line 3,298: | Line 3,298: | ||
<Character name="Emily">Short & shrift</Character> |
<Character name="Emily">Short & shrift</Character> |
||
</CharacterRemarks> |
</CharacterRemarks> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">sign$ = "<,<,>,>,&,&" |
||
dim sign$(1) |
dim sign$(1) |
||
long = token(sign$, sign$(), ",") |
long = token(sign$, sign$(), ",") |
||
Line 3,346: | Line 3,346: | ||
next i |
next i |
||
print xml_CharacterRemarks$(testset$())</ |
print xml_CharacterRemarks$(testset$())</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn xmlEscape(text){ |
||
text.replace(" &"," &") .replace(" \""," "") |
text.replace(" &"," &") .replace(" \""," "") |
||
.replace(" '"," '") .replace(" <"," <") .replace(" >"," >") |
.replace(" '"," '") .replace(" <"," <") .replace(" >"," >") |
||
Line 3,366: | Line 3,366: | ||
0'|Burns: "When chapman billies leave the street ..."|, |
0'|Burns: "When chapman billies leave the street ..."|, |
||
"Short & shrift")) |
"Short & shrift")) |
||
.print();</ |
.print();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |