how to nest tags using a conditional
Wednesday, 19 February 2020 · 3 messages
MH
Quoted text · thufir · 36 lines
Am 19.02.2020 um 15:08 schrieb thufir: > How can I start a new "record" and then nest tags in that record? > > > but I'm getting output like: > > <xml> > <record> > if (matches($line, "[0-9]")) > then <data>people</data> > else <name>people</name> > </record> > <record> > if (matches($line, "[0-9]")) > then <data>joe</data> > else <name>joe</name> > </record> > .. > > wheras I just want output like: > > <record> > <name>joe</name> > <data>123</data> > </record> > > > the query: > > xquery version "3.0"; > > <xml> > { > for $line in db:open("foo.txt")//text() > return > <record>
Nest any contained expression in further curly braces
{Quoted text · thufir · 3 lines
> if (matches($line, "[0-9]")) > then <data>{$line}</data> > else <name>{$line}</name>
}
Quoted text · thufir · 7 lines
> </record> > } > </xml> > > > >
T
thufir19 Feb 2020 16:08 #
How can I start a new "record" and then nest tags in that record?
but I'm getting output like:
<xml>
<record>
if (matches($line, "[0-9]"))
then <data>people</data>
else <name>people</name>
</record>
<record>
if (matches($line, "[0-9]"))
then <data>joe</data>
else <name>joe</name>
</record>
..wheras I just want output like:
<record>
<name>joe</name>
<data>123</data>
</record>the query:
xquery version "3.0";
<xml>
{
for $line in db:open("foo.txt")//text()
return
<record>
if (matches($line, "[0-9]"))
then <data>{$line}</data>
else <name>{$line}</name>
</record>
}
</xml>I think it's a matter of using the () and {} correctly. Pardon, yes, I'm literally reading a book on this, still trying to understand the syntax.
thanks,
Thufir
T
Quoted text · Martin Honnen · 54 lines
On 2020-02-19 6:10 a.m., Martin Honnen wrote: > Am 19.02.2020 um 15:08 schrieb thufir: >> How can I start a new "record" and then nest tags in that record? >> >> >> but I'm getting output like: >> >> <xml> >> <record> >> if (matches($line, "[0-9]")) >> then <data>people</data> >> else <name>people</name> >> </record> >> <record> >> if (matches($line, "[0-9]")) >> then <data>joe</data> >> else <name>joe</name> >> </record> >> .. >> >> wheras I just want output like: >> >> <record> >> <name>joe</name> >> <data>123</data> >> </record> >> >> >> the query: >> >> xquery version "3.0"; >> >> <xml> >> { >> for $line in db:open("foo.txt")//text() >> return >> <record> > > > Nest any contained expression in further curly braces > > { > >> if (matches($line, "[0-9]")) >> then <data>{$line}</data> >> else <name>{$line}</name> > > > } > >> </record> >> } >> </xml> >>
that was quite helpful, thanks. I'm getting:
<name>joe</name>
</record>
<record>
<data>phone1</data>
</record>
<record>
<data>phone2</data>
</record>and want to only open the new record tab for something like:
<record>
<name>joe</name>
<data>phone1</data>
<data>phone2</data>
</record>but get "incomplete if statement" when I try to add open and close record tags inside each if statement.
xquery version "3.0";
<xml>
{
for $line in db:open("foo.txt")//text()
return
<record>
{
if (matches($line, "[0-9]"))
then <data>{$line}</data>
else <name>{$line}</name>
}
</record>
}
</xml>thanks,
Thufir
Written for BaseX 9.3. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery