xml.getAddressFromXpath()
Bill Humphries
whump at apple.com
Mon Mar 18 17:28:45 PST 2002
Here's a proposed verb for Radio/Frontier, as well as first pass at
writing it.
There's already an xml.getPathAddress() verb implemented as a script. I
need to take XPath expressions and get the corresponding table address,
thus, I wrote getAddressFromXpath() based on xml.getPathAddress().
In the first version of getAddressFromXpath(), you're limited to simple
Xpath qualifiers, /a/b[@c = '2']/d. I haven't implemented the whole suite
of XPath functions, or XPath's axes. But it does what I need it to do for
the moment.
The reason I'm doing this is because I want the part of the system I'm
working on that faces the Web to use standard interfaces. XPath is a
standard for describing the location in an XML document. So when the
request hits the system, we convert it to a Frontier table address and
crank from there.
on getAddressFromXpath (xtable, path, adrresult) {
«3/18/02; 3:26:29 PM by whump
«Based on xml.getPathAddress by DW
«Given an XPath expression, and an XML table, locate the address
corresponding to the expression.
«If found, return it in adrresult^
«Limitiations
«XPath Axes are not supported
«XPath functions are not supported
«[@foo = 'bar'] is supported
«10/14/98; 7:46:15 AM by DW
«xtable is the address of an xml structure
«from the root of the table, travel from the top down the
/-separated path
«something like "/ticket/header/title"
«set adrresult^ to point to the value at the end of the path and
return true
«return false if the path is invalid
local {
nomad = xtable;
name;
temp;
expression;
qualified;
attribute;
value};
loop {
qualified = false;
if path beginswith "/" {
path = string.delete (path, 1, 1)};
if path == "" {
break};
name = string.nthfield (path, "/", 1);
if (name contains "[") and (name endsWith "]") { // path is qualified?
temp = name;
bundle { // split into element name and qualifier, attribute
and value
bra = string.patternMatch('[',temp);
expression = string.mid(temp,bra,sizeOf(temp) - 1);
name = string.mid(temp,1,bra - 1);
temp = expression;
attribute =
string.popLeading(string.popLeading(string.nthField(temp," ",1),"["),"@");
value =
string.popTrailing(string.replaceAll(string.nthField(temp," ",3),"'",""),"
]");
qualified = true;
path = string.replace(path,expression,"")}};
path = string.delete (path, 1, sizeof (name) + 1);
try {
if qualified {
found = false;
elements = xml.getAddressList(nomad,name);
for i = 1 to sizeOf(elements) {
if xml.getAttributeValue(elements[i],attribute) == string(value) {
found = true;
nomad = elements[i]; // keep searching from this point
break}};
if not(found) {
return false}}
else {
nomad = xml.getAddress (nomad, name)}}
else {
return (false)}};
adrresult^ = nomad;
return (true)};
«bundle //test code
«dialog.alert (getAddressFromXpath (@scratchpad.test,
"/page/howdoi[@id = '1']/para[@id = '1']/em", at scratchpad.foo)^)
----
Bill Humphries <whump at apple.com>
Webmaster, HR Systems
Apple Computer
More information about the Frontier-Users
mailing list