XML interpolation syntax
Syntax extensions for XML > XML literals

Prev:[XML


XML interpolation has two forms for each of the words <XML and [XML: a fry-like form and a locals form. To splice locals in, use the syntax <-variable->. To splice something in from the stack, in the style of fry, use the syntax <->. An XML interpolation form may only use one of these styles.

These forms can be used where a tag might go, as in [XML <foo><-></foo> XML] or where an attribute might go, as in [XML <foo bar=<->/> XML]. When an attribute is spliced in, it is not included if the value is f and if the value is not a string, the value is put through present. Here is an example of the fry style of XML interpolation:
USING: splitting xml.writer xml.syntax ; "one two three" " " split [ [XML <item><-></item> XML] ] map <XML <doc><-></doc> XML> pprint-xml
<?xml version="1.0" encoding="UTF-8"?> <doc> <item> one </item> <item> two </item> <item> three </item> </doc>

Here is an example of the locals version:
USING: locals urls xml.syntax xml.writer ; [let 3 :> number f :> false URL" http://factorcode.org/" :> url "hello" :> string \ drop :> word <XML <x number=<-number-> false=<-false-> url=<-url-> string=<-string-> word=<-word-> /> XML> pprint-xml ]
<?xml version="1.0" encoding="UTF-8"?> <x number="3" url="http://factorcode.org/" string="hello" word="drop"/>

XML interpolation can also be used, in conjunction with inverse in pattern matching. For example:
USING: xml.syntax inverse ; : dispatch ( xml -- string ) { { [ [XML <a><-></a> XML] ] [ "a" prepend ] } { [ [XML <b><-></b> XML] ] [ "b" prepend ] } { [ [XML <b val='yes'/> XML] ] [ "yes" ] } { [ [XML <b val=<->/> XML] ] [ "no" prepend ] } } switch ; [XML <a>pple</a> XML] dispatch write
apple