How to dynamically search/replace text with update in XQuery (exist-db) - xquery

My intention is to somehow clean source files automatically. How to do that in XQuery? (I am not interested in reconstructing the document in memory and storing it as a new one.) It is quite easy to do something similar in case of short and simple elements addressed directly, however, I can’t figure out how to do that dynamically for all the text nodes, if possible.
I would expect something like this could work:
update replace $div[contains(., 'chapter')] with replace(., 'chapter', 'Chapter')
This throws err:XPDY0002 Undefined context sequence for 'self::node()' [source: String]
Apparently, there is a problem in addressing the context with . in the replacing function. But maybe I don’t understand the update thing in general. I am only inspired by the bottom of this article.

Expression to the right of with is independent from expression to the left. So an explicit node/context is needed on both part :
update replace $div[contains(., 'chapter')] with replace($div, 'chapter', 'Chapter')

Related

Creating a new string literal type based on another

The CSS.Pseudo types of csstypes have a bunch of CSS selectors like for ':hover' , ':active' and so on.
Is it possible to create a new type based on CSS.Pseudo, which accepts all the CSS.Pseudo types but with a '&' in front?
For example '&:hover', '&:active' and so on.
I want to be able to do something like
type CSS.Pseudo = ':hover' | ':active' ... etc.
type CssStyle= { [SOMETHINGSOMETHING CSS.Pseudo]: string}
const styles: CssStyle= {
'#:hover: someCssString, //OK
':hover: someCssString, //NOT OK
'&:hov': someCssString //NOT OK
}
I've tried a billion things on TS Playground but I feel I'm just not good enough at TS or it's just impossible.
You can't do this programmatically, at least as of TypeScript 3.5.
This has been suggested before, and the suggestion was closed as a duplicate of either a string literal key augmentation or a regular expression string literal validation suggestion, neither of which are particularly close to being incorporated into the language. If you really want to see this happen, you might want to go to one or both of those issues and give them a 👍 or possibly describe your use case if you think it's more compelling than what's already there.
Sorry this isn't the answer you probably want, but at least you can stop trying to get the compiler to do this for you. Good luck!

Ada83 Constraint Error not Present in Watch Window

I'm looking at a constraint error when running this code. In the debugger it halts on the 2nd line (Menu_Text...). I put the code on the RHS of the assignment into the watch window and I see no problem. It's evaluates exactly as it should.
for I in 1..This_Info_Ptr.Child_Menu_Length loop
Menu_Text := This_Info_Ptr.Child_Menu_Text_Ptr.all(I-1);
Menu_State := This_Info_Ptr.Child_Menu_States_Ptr.all(I-1);
...
The error is when I is 1. I have confirmed that this code works in the watch window:
This_Info_Ptr.Child_Menu_Text_Ptr.all(I-1)
Child_Menu_Text_Ptr and Child_Menu_States_Ptr point to arrays (of strings and enums),
How should I debug this I general? I can't see anything wrong with the code. However, my familiarity with Ada access types is limited. The ..._Ptr variables are access types.
I am using GNAT.
Assuming the arrays being indexed are all of the same dimension and have the same index type, then iterating over them should be done using the 'first & 'last or 'range attributes.
It is likely that the hand coded control of length values has a bug.
Using the inbuilt functionality is safer and more reliable.
You aren't showing enough source text to tell us for sure what is happening. IsThis_Info_Ptr.Child_Menu_Text_Ptr.all a function or an array? What is it's specification?
If it is an array, you should remember that Ada allows arrays to be indexed by any discrete type, and that arrays in Ada always know their own indexing bounds.

Plone Conditional - If Content Type is Versionable

Is there a simple way to check if a content-type, or a specific object, has Versioning enabled/disabled in Plone (4.3.2)?
For context, I am making some unique conditionals around portal_actions. So instead of checking path('object/##iterate_control').checkout_allowed(), I need to first see if versioning is even enabled. Otherwise, the action in question does not display for items that have versioning disabled, because obviously it isn't checkout_allowed.
I didn't have any luck with good ole Google, and couldn't find this question anywhere here, so I hope it's not a dupe. Thanks!
I was able to get this working by creating a new script, importing getToolByName, and checking current content type against portal_repository.getVersionableContentTypes(). Then just included that script in the conditional.
I was looking for something like this that already existed, so if anyone knows of one let me know. Otherwise, I've got my own now. Thanks again!
The first thing that checkout_allowed does is check if the object in question supports versioning at all:
if not interfaces.IIterateAware.providedBy(context):
return False
(the interface being plone.app.iterate.interfaces.IIterateAware:
class IIterateAware( Interface ):
"""An object that can be used for check-in/check-out operations.
"""
The semantics Interface.providedBy(instance) are a bit unfortunate for usage in conditions or TAL scripts, because you'd need to import the interface, but there's a reversal helper:
context.portal_interface.objectImplements(context,
'plone.app.iterate.interfaces.IIterateAware')

TAL Condition in Plone to hide HTML if it's a Document (Page)

I'm trying to modify my /portal_view_customizations/zope.interface.interface-plone.belowcontenttitle.documentbyline template with a tal expression, so that the document's author and the modification date do not show if the current portal type is a Document (Page). I don't mind if it shows for News Items, which are time sensitive, but not the Documents/Pages.
This is my failing Plone TAL expression:
<div class="documentByLine"
id="plone-document-byline"
i18n:domain="plone"
tal:condition="view/show and not:python:here.portal_type == 'Document'">
...
I've also tried:
<div class="documentByLine"
id="plone-document-byline"
i18n:domain="plone"
tal:condition="view/show and not:context/portal_type='Document'">
but still no luck. The tracebacks are pretty cryptic and don't relate to the TAL expression. However, if I get rid of my condition for portal_type, then it works again. Any thoughts are appreciated. A manual would be good, but I've looked at the official ones, and they don't mention this.
TAL's underlying TALES, the expression-engine of which TAL makes use of, doesn't support the mixing of expression-types in one expression. It's syntax allows only one expression-type to be specified (defaults to 'path', if omitted, btw), as no delimiter is provided (like a semicolon for chaining several TAL-statements in one element, e.g.).
But instead of mixing three expression-types you can use one python-expression, try:
python: view.show and context.portal_type()!='Document'
Update:
If you have customized a Plone's default-template via portal_view_customizations ('TTW'), now restricted python-methods cannot be accessed, that' why view/show throws an error.
It returns the allowAnonymousViewAbout-property of the site-properties, you can also check this condition yourself and your expression looks like:
tal:define="name user/getUserName"
tal:condition="python:test(name!='Anonymous User') and context.portal_type()!='Document';
Quick'n'dirty alternative:
Do it with CSS:
body.userrole-anonymous .documentByLine {display:none}
body:not(.template-document_view) .documentByLine {display:block}
This will render the hidden elements, but it's helpful for prototyping and such, or 'quickfixes' (no admin available, etc.).

Is it possible to declare a default associative array in Smarty?

In Smarty, I know you can declare a string:
{$somevar|default:'some string'}
or even an array:
{$somevar|default:array('someval')}
How do you/Is it possible to set an associative array as a default value? as this doesn't seem to work:
{$somevar|default:array('default'=>array('subkey'=>'subval'))}
I just tried:
{$somevar|default:array('key'=>'val')}
It's the '=>' smarty doesn't like
I know its probably not the solution you're looking for, but you can always just use the {php} feature. However, I will try a few things and see if I can work the format out.
Just out of interest, why are you trying to do this in the tpl file and not in the calling PHP script?
Edit
From takeing a read, it doesn't seem like it is possible. However, there is a "set" plugin which allows it, see here (bottom example).

Resources