XML/Output: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(5 intermediate revisions by 2 users not shown)
Line 954:
 
=={{header|C}}==
==={{libheader|libXML}}===
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
Line 995:
return EXIT_SUCCESS;
}</syntaxhighlight>
 
==={{libheader|Gadget}}===
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
const char* codes[] = {"&amp;","&gt;","&lt;"};
const char* repl[] = {"&",">","<"};
char* change_codes( const char* xml_line )
{
int i;
String xml;
Let( xml, xml_line );
Iterator up i [0:1:3]{
Get_fn_let( xml, Str_tran( xml, repl[i], codes[i] ) );
}
return xml;
}
char* generate_xml( const char* names[], int lnames, const char* remarks[] )
{
String xml;
char attrib[100];
int i;
Iterator up i [0:1:lnames]{
String remk;
Get_fn_let ( remk, change_codes( remarks[i] ) );
sprintf(attrib,"name=\"%s\"",names[i]);
if(!i) {
Get_fn_let ( xml, Parser("Character", attrib, remk, NORMAL_TAG ));
}else{
Get_fn_cat ( xml, Parser("Character", attrib, remk, NORMAL_TAG ));
}
Free secure remk;
}
Get_fn_let ( xml, Parser("CharacterRemarks", "", xml, NORMAL_TAG ));
Get_fn_let ( xml, Str_tran(xml,"><",">\n<") );
return xml;
}
 
#define alen(_X_) ( sizeof(_X_) / sizeof(const char*) )
Main
const char *names[] = {
"April", "Tam O'Shanter", "Emily"
};
const char *remarks[] = {
"Bubbly: I'm > Tam and <= Emily",
"Burns: \"When chapman billies leave the street ...\"",
"Short & shrift"
};
char* xml = generate_xml( names, alen(names), remarks );
Print "%s\n", xml;
Free secure xml;
End
</syntaxhighlight>
{{out}}
 
$ ./tests/RC_xmlout
<syntaxhighlight lang="xml">
<CharacterRemarks>
<Character name="April">Bubbly: I'm &gt; Tam and &lt;= Emily</Character>
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character>
<Character name="Emily">Short &amp; shrift</Character>
</CharacterRemarks>
</syntaxhighlight>
 
=={{header|C sharp}}==
Line 1,768 ⟶ 1,835:
] XMLOutput.
</syntaxhighlight>
 
=={{header|jq}}==
{{trans|Wren}}
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
<syntaxhighlight lang=jq>
def escapes: [
["&" , "&amp;"], # must do this one first
["\"", "&quot;"],
["'" , "&apos;"],
["<" , "&lt;"],
[">" , "&gt;"]
];
 
def xmlEscape:
reduce escapes[] as $esc (.; gsub($esc[0]; $esc[1]));
 
def xmlDoc(names; remarks):
reduce range(0;names|length) as $i ("<CharacterRemarks>\n";
(names[$i]|xmlEscape) as $name
| (remarks[$i]|xmlEscape) as $remark
| . + " <Character name=\"\($name)\">\($remark)</Character>\n")
+ "</CharacterRemarks>" ;
 
def names: ["April", "Tam O'Shanter", "Emily"];
def remarks: [
"Bubbly: I'm > Tam and <= Emily",
"Burns: \"When chapman billies leave the street ...\"",
"Short & shrift"
];
 
xmlDoc(names; remarks)
</syntaxhighlight>
{{output}}
As for [[#Wren|Wren]].
 
=={{header|Julia}}==
Line 3,147 ⟶ 3,249:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var escapes = [
["&" , "&amp;"], // must do this one first
["\"", "&quot;"],
Line 3,188 ⟶ 3,290:
{{libheader|Wren-xsequence}}
Alternatively, using an XML library.
<syntaxhighlight lang="ecmascriptwren">import "./xsequence" for XDocument, XElement, XAttribute
 
var createXmlDoc = Fn.new { |names, remarks|
9,485

edits