XML/XPath: Difference between revisions

2,227 bytes added ,  1 month ago
Added FreeBASIC
m (syntax highlighting fixup automation)
(Added FreeBASIC)
 
(2 intermediate revisions by 2 users not shown)
Line 1,454:
"Blork and Freen Instameal","Grob winglets"]
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Dim As String XML, item, price, nombre
Dim As Integer P1, P2
 
'' Read the XML file
Open "test3.xml" For Input As #1
XML = Input(Lof(1), #1)
Close #1
 
'' Find the first 'item' element
P1 = Instr(XML, "<item ")
P2 = Instr(XML, "</item>")
item = Mid(XML, P1, P2-P1+7)
Print "The first 'item' element is:"
Print item
 
'' Find all 'price' elements
Print "The 'prices' are:"
P1 = 1
Do
P1 = Instr(P1, XML, "<price>")
If P1 = 0 Then Exit Do
P2 = Instr(P1, XML, "</price>")
price = Mid(XML, P1+7, P2-P1-7)
Print price
P1 = P2 + 1
Loop
 
'' Find all 'nombre' elements
Print !"\nThe 'names' are:"
P1 = 1
Do
P1 = Instr(P1, XML, "<name>")
If P1 = 0 Then Exit Do
P2 = Instr(P1, XML, "</name>")
nombre = Mid(XML, P1+6, P2-P1-6)
Print nombre
P1 = P2 + 1
Loop
 
Sleep</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 3,771 ⟶ 3,813:
{{libheader|Wren-pattern}}
Wren does not have built-in support for XML/XPath and, to my knowledge, there is no third party module which adds such support either. We therefore have to fall back on string pattern matching to perform the required queries.
<syntaxhighlight lang="ecmascriptwren">import "./pattern" for Pattern
 
var doc = """
Line 3,846 ⟶ 3,888:
 
Note that this library doesn't support XPath as such though for this particular task simple 'for' statements suffice.
<syntaxhighlight lang="ecmascriptwren">import "./xsequence" for XDocument
 
var xml = """
Line 3,891 ⟶ 3,933:
System.print("\nThe 'prices' are:\n%(prices.join("\n"))")
System.print("\nThe 'names' are:\n%(names.join("\n"))")</syntaxhighlight>
 
=={{header|XPL0}}==
To run: xml <xml.xml
<syntaxhighlight lang "XPL0">include xpllib; \for StrFind
 
char XML(1000000), P, P1, P2, Addr;
int I, Ch;
[I:= 0;
loop [Ch:= ChIn(1);
XML(I):= Ch;
if Ch = $1A \EOF\ then quit;
I:= I+1;
];
XML(I):= 0;
 
P1:= StrFind(XML, "<item ");
P2:= StrFind(XML, "</item>");
Text(0, "The first 'item' element is:^m^j");
for P:= P1 to P2+6 do ChOut(0, P(0));
CrLf(0);
 
Text(0, "^m^jThe 'prices' are:^m^j");
Addr:= XML;
loop [P1:= StrFind(Addr, "<price>");
if P1 = 0 then quit;
P2:= StrFind(Addr, "</price>");
if P2 = 0 then quit;
for P:= P1+7 to P2-1 do ChOut(0, P(0));
CrLf(0);
Addr:= P2+1;
];
Text(0, "^m^jThe 'names' are:^m^j");
Addr:= XML;
loop [P1:= StrFind(Addr, "<name>");
if P1 = 0 then quit;
P2:= StrFind(Addr, "</name>");
if P2 = 0 then quit;
for P:= P1+6 to P2-1 do ChOut(0, P(0));
CrLf(0);
Addr:= P2+1;
];
]</syntaxhighlight>
{{out}}
<pre>
The first 'item' element is:
<item upc="123456789" stock="12">
<name>Invisibility Cream</name>
<price>14.50</price>
<description>Makes you invisible</description>
</item>
 
The 'prices' are:
14.50
23.99
4.95
3.56
 
The 'names' are:
Invisibility Cream
Levitation Salve
Blork and Freen Instameal
Grob winglets
</pre>
 
=={{header|XProc}}==
2,122

edits