]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/traversal/traversal-docs.factor
Solution to Project Euler problem 65
[factor.git] / basis / xml / traversal / traversal-docs.factor
1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: help.markup help.syntax xml.data sequences strings multiline ;
4 IN: xml.traversal
5
6 ABOUT: "xml.traversal"
7
8 ARTICLE: "xml.traversal" "Utilities for traversing XML"
9     "The " { $vocab-link "xml.traversal" } " vocabulary provides utilities for traversing an XML DOM tree and viewing the contents of a single tag. The following words are defined:"
10     $nl
11     { $subsection { "xml.traversal" "intro" } }
12     { $subsection tag-named }
13     { $subsection tags-named }
14     { $subsection deep-tag-named }
15     { $subsection deep-tags-named }
16     { $subsection get-id }
17     "To get at the contents of a single tag, use"
18     { $subsection children>string }
19     { $subsection children-tags }
20     { $subsection first-child-tag }
21     { $subsection assert-tag } ;
22
23 ARTICLE: { "xml.traversal" "intro" } "An example of XML processing"
24 "To illustrate how to use the XML library, we develop a simple Atom parser in Factor. Atom is an XML-based syndication format, like RSS. To see the full version of what we develop here, look at " { $snippet "basis/syndication" } " at the " { $snippet "atom1.0" } " word. First, we want to load a file and get a DOM tree for it."
25 { $code <" "file.xml" file>xml "> }
26 "No encoding descriptor is needed, because XML files contain sufficient information to auto-detect the encoding. Next, we want to extract information from the tree. To get the title, we can use the following:"
27 { $code <" "title" tag-named children>string "> }
28 "The " { $link tag-named } " word finds the first tag named " { $snippet "title" } " in the top level (just under the main tag). Then, with a tag on the stack, its children are asserted to be a string, and the string is returned." $nl
29 "For a slightly more complicated example, we can look at how entries are parsed. To get a sequence of tags with the name " { $snippet "entry" } ":"
30 { $code <" "entry" tags-named "> }
31 "Imagine that, for each of these, we want to get the URL of the entry. In Atom, the URLs are in a " { $snippet "link" } " tag which is contained in the " { $snippet "entry" } " tag. There are multiple " { $snippet "link" } " tags, but one of them contains the attribute " { $snippet "rel=alternate" } ", and the " { $snippet "href" } " attribute has the URL. So, given an element of the sequence produced in the above quotation, we run the code:"
32 { $code <" "link" tags-named [ "rel" attr "alternate" = ] find nip "> }
33 "to get the link tag on the stack, and"
34 { $code <" "href" attr >url "> }
35 "to extract the URL from it." ;
36
37 HELP: deep-tag-named
38 { $values { "tag" "an XML tag or document" } { "name/string" "an XML name or string representing a name" } { "matching-tag" tag } }
39 { $description "Finds an XML tag with a matching name, recursively searching children and children of children." }
40 { $see-also tags-named tag-named deep-tags-named } ;
41
42 HELP: deep-tags-named
43 { $values { "tag" "an XML tag or document" } { "name/string" "an XML name or string representing a name" } { "tags-seq" "a sequence of tags" } }
44 { $description "Returns a sequence of all tags of a matching name, recursively searching children and children of children." }
45 { $see-also tag-named deep-tag-named tags-named } ;
46
47 HELP: children>string
48 { $values { "tag" "an XML tag or document" } { "string" "a string" } }
49 { $description "Concatenates the children of the tag, throwing an exception when there is a non-string child." } ;
50
51 HELP: children-tags
52 { $values { "tag" "an XML tag or document" } { "sequence" sequence } }
53 { $description "Gets the children of the tag that are themselves tags." }
54 { $see-also first-child-tag } ;
55
56 HELP: first-child-tag
57 { $values { "tag" "an XML tag or document" } { "tag" tag } }
58 { $description "Returns the first child of the given tag that is a tag." }
59 { $see-also children-tags } ;
60
61 HELP: tag-named
62 { $values { "tag" "an XML tag or document" }
63     { "name/string" "an XML name or string representing the name" }
64     { "matching-tag" tag } }
65 { $description "Finds the first tag with matching name which is the direct child of the given tag." }
66 { $see-also deep-tags-named deep-tag-named tags-named } ;
67
68 HELP: tags-named
69 { $values { "tag" "an XML tag or document" }
70     { "name/string" "an XML name or string representing the name" }
71     { "tags-seq" "a sequence of tags" } }
72 { $description "Finds all tags with matching name that are the direct children of the given tag." }
73 { $see-also deep-tag-named deep-tags-named tag-named } ;
74
75 HELP: get-id
76 { $values { "tag" "an XML tag or document" } { "id" "a string" } { "elem" "an XML element or f" } }
77 { $description "Finds the XML tag with the specified id, ignoring the namespace." } ;