XQuery - How to remove empty subelement - xquery

I have the following XQuery:
<a> {
for $p in doc("p.xml")//p
return
<p q="{$p/#q}> {
for $w in $p//#w
where count(doc("r.xml")//s[#w = $w]) = 0
return
<s w="{$w}"></s>
} </p>
} </a>
When I run galax-run, it will give the following result (After I formatted it):
<a>
<p q="p1">
<s w="S"/>
<s w="Sc"/>
<s w="L"/>
</p>
<p q="p2"/>
<p q="p3">
<s w="S"/>
</p>
</a>
I want to delete <p q="p2"/> in the result because there is nothing in the element. So the result I want is:
<a>
<p q="p1">
<s w="S"/>
<s w="Sc"/>
<s w="L"/>
</p>
<p q="p3">
<s w="S"/>
</p>
</a>
How do I modify the XQuery in order to get this result? Thank you in advance!

It isn't clear without sample input XML, but I think, you can use if-then-else construct to skip <p> elements which $content would be empty. Something like this (untested) :
<a> {
for $p in doc("p.xml")//p
let $content :=
for $w in $p//#w
where count(doc("r.xml")//s[#w = $w]) = 0
return $w
return
if($content) then
<p q="{$p/#q}"> {
for $c in $content
return
<s w="{$c}"></s>
} </p>
else ()
} </a>

Related

Loop in XQuery repeats results several times

I have a function which returns paragraphs from the text. So I'm comparing the <anchor> tag's attribute number(#n) to the <notes> tag's attribute number, if it's the same I want to print it with the tooltip if not I just want to print out the paragraph.
declare function letter:text_trans($node as node(), $model as map(*))
{
let $resource := collection('/db/apps/Tobi-oshki/data')
for $ab in $resource//tei:div[#type="translation"]/tei:ab
for $note in $resource//tei:div[#type="notes"]/tei:note
return
if (data($note/#n) eq data($ab/tei:anchor/#n))
then
<div class="tooltip">{$ab}
<span class="tooltiptext"> {data($note/#n)}.{$note}</span>
</div>
else
<p> {$ab} </p>
};
In <notes> I have three notes and when it loops over the notes, each paragraph is returned three times.
How can I change it so it returns the paragraphs only one time?
I am using xquery version "3.1";
Inside the for loop on the $ab, let a variable for the $note and select notes that have #n attribute values that match the $ab, then if there is a matching $note, use it, else return the <p> using just the $ab:
let $resource := collection('/db/apps/Tobi-oshki/data')
for $ab in $resource//tei:div[#type="translation"]/tei:ab
let $note := $resource//tei:div[#type="notes"]/tei:note[#n = $ab/tei:anchor/#n]
return
if ($note)
then
<div class="tooltip">{$ab}
<span class="tooltiptext"> {data($note/#n)}.{$note}</span>
</div>
else
<p> {$ab} </p>
With this input:
<tei:doc>
<tei:div type="notes">
<tei:note n="1">note1</tei:note>
<tei:note n="2">note2</tei:note>
</tei:div>
<tei:div type="translation">
<tei:ab><tei:anchor n="1">translated note1</tei:anchor></tei:ab>
<tei:ab><tei:anchor n="3">translated note3</tei:anchor></tei:ab>
</tei:div>
</tei:doc>
the code above produces this output:
<div class="tooltip">
<tei:ab xmlns:tei="tei">
<tei:anchor n="1">translated note1</tei:anchor>
</tei:ab>
<span class="tooltiptext">1.<tei:note n="1" xmlns:tei="tei">note1</tei:note>
</span>
</div>
<p>
<tei:ab xmlns:tei="tei"><tei:anchor n="3">translated note3</tei:anchor></tei:ab>
</p>

ACF into shortcode

Hello good people of StackOverflow, I was wondering is there any way I can use advanced custom fields in shortcode?
function highlight($atts) {
return '
<div class="col-lg-6 ">
<div class="highlighted">
<p class="page-title">TEST</p>
</div>
</div>';
}
add_shortcode('scbox', 'highlight');
so I would like to put something like {{ the_field('text') }} where the "TEST" is now, I'm using blade template if its of any help
You can use the get_field() function to obtain the value that you are looking for. (get_field() will return the value, whereas the_field() will print the value wherever invoked.)
You can then concatenate the value into your returned string:
function highlight($atts) {
$text = get_field('text');
return '
<div class="col-lg-6 ">
<div class="highlighted">
<p class="page-title">' . $text . '</p>
</div>
</div>';
}

Experiencing tags on screen

I am calling title, body and few other things from database and using echo to display it on screen. Everything is working fine except body, where p tags are printed at start and end of body. My code is
echo "<div class='db'>
<h2 style='background-color:#ffffff;color:#000000;'>".$row['title']."</h2>
<p style='background-color:#ffffff'>" .
(strlen($row['body'])>500?
substr( $row['body'],0,500)."...<a href='blog.php?blogId=" . $row['blogs_id'] . "' style='font-size:13px;color:black;background-color:#ffffff;text-decoration:none;'>Read more</a>":
$row['body']).
"</p>
<img src='$profilephoto_info' height='60' style='background-color:#ffffff;border-radius:20px;'>
By:<a href='$posted_by' style='background-color:#ffffff;text-decoration:none;color:#626769;font-size:13px;'>$posted_by</a>
<p style='background-color:#ffffff;text-decoration:none;color:#626769;font-size:13px;''>".$row['bio']."</p>
</div>";
No h2 tags appear for title nor for any other elements only body is showing p tags around it on screen.
Format your code to know where things get missed up
consider heredoc
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
also loss the inline styles!
<?php
$title = $row['title'];
$shortBody = strlen($row['body']) >500 ? substr( $row['body'],0,500) : $row['body'];
$url = "blog.php?blogId=" . $row[' blogs_id '];
$bio = $row['bio '];
echo <<<dbElement
<div class='db'>
<h2 style='background-color:#FFF;color:#000;'>
$title
</h2>
<p style='background-color:#FFF'>
$shortBody ...
<a href="$url"
style="font-size:13px;color:black;background-color:#FFF;text-decoration:none;">
Read more
</a>
</p>
<img src='$profilephoto_info' height='60'
style='background-color:#FFF;border-radius:20px;'>
By:
<a href='$posted_by'
style='background-color:#FFF;text-decoration:none;color:#626769;font-size:13px;'>
$posted_by
</a>
<p
style='background-color:#FFF;text-decoration:none;color:#626769;font-size:13px;'>
$bio
</p>
</div>
dbElement;
?>
*make sure to not include my php opening and closing tags (<?php and ?>)

Remove line from code

how i can remove line under Latest film and News if you look http://filmsground.net/
//Latest film start
function latestfilm ()
{
$query = mysql_query("SELECT * FROM filmdb ORDER BY id DESC LIMIT 1");
while($fetch=mysql_fetch_assoc($query))
{
echo '<h2> <div id="textbox">
<p class="alignright"><span style="color:#3399ff">News <img title="New News corner" src="/img/new.png" style="height:15px; margin-bottom:-3px; margin-top:-3px; width:15px" />
<p class="alignleft">Latest film added: <span style="color:#FFA500;">';
echo '<a class="movie" href="?inav=movie&id='.$fetch['id'].'">'.$fetch['title'].' ('.$fetch['year'].')';
echo '</span><strong><div style="clear: both;"></div><hr class="bevel" /></div></div>';
}
}
//Latest film end
Remove the <hr class="bevel">
Your last echo should be echo '</span><strong><div style="clear: both;"></div></div></div>';

xquery- how to assign a number(in sequence) when obtaining a number of records via single query using FLWOR expression

While trying to process and obtain data from an XML document, I want to obtain a number of records using a single FLWOR expression-- I am doing this by using the 'let' clause to obtain the data-- so I have a number of rows of data.
Eg.
<div id="main">
<p> Text #1</p>
<p> Text #2</p>
<p> Text #3</p>
<p> Text #4</p>
</div>
Now, I understand how to get the 4 'p' elements -- however I would like to also assign a sequence number to each line -- viz. I want to obtain the text this way--
<data>Text #1</data><sequence> Sequence #1</sequence>
<data>Text #2</data><sequence> Sequence #2</sequence>
<data>Text #3</data><sequence> Sequence #3</sequence>
<data>Text #4</data><sequence> Sequence #4</sequence>
There sure is a more elegant way than doing it like this.... but it works:
let $t := <div id="main">
<p>Text A</p>
<p>Text B</p>
<p>Text C</p>
<p>Text D</p>
</div>
for $pos in $t/p/position()
let $p := $t/p[$pos]
return (
<data>{ $p/text() }</data>,
<sequence>Sequence { $pos }</sequence>
)
While I'm not completely sure what string value you would to have for the <sequence/> element, one solution could look as follows:
for $p in <div id="main">
<p>Text #1</p>
<p>Text #2</p>
<p>Text #3</p>
<p>Text #4</p>
</div>/p
return (
<data>{ $p/text() }</data>,
<sequence>Sequence { replace($p, 'Text ', '') }</sequence>
)
You can use the at keyword in the FLWOR expression's for clause:
declare variable $div :=
<div id="main">
<p> Text #1</p>
<p> Text #2</p>
<p> Text #3</p>
<p> Text #4</p>
</div>;
for $p at $pos in $div/p
return (
<data>{$p/text()}</data>,
<sequence>Sequence #{$pos}</sequence>
)

Resources