I would like to write Nodes like
<name>Peter</name>
(with start and end tag) into a QDomDocument.
When I create QDomElements and append them as child to a parent element:
QDomElement node = doc.createElement("node");
parent.appendChild(node);
They are added as
<node/>
to the parent element. The parent automatically gets a start and end tag so the file would look like this:
<parent>
<node/>
</parent>
But how do I add a value to my node so that it looks like I want it (with value between start and end tag). Adding a new QDomElement as child to node it would just look like . Adding attribute would show up like ?
Would be great if anyone could help me! Thanks!
Create a text node using DOM Document, and add it to your newly created element as a child:
QDomElement node = doc.createElement("name");
parent.appendChild(node);
// Now, add a text element to your node
node.appendChild( doc.createTextNode( "Peter"));
Related
Is there any way to get access to the ROOT node from within another context?
The example above is just to show my intention. Getting access to ROOT_NODE without using '../../..' since changes in xml could break that type of selector.
XSLT
<div class="column">
<xsl:for-each select="languages/server/elem">
<!-- Context is ELEM node -->
<div>
<!-- How can I get access to the ROOT_NODE ?-->
<span class="text"><xsl:value-of select="ROOT_NODE/#title"/></span>
<!-- Print ELEM text -->
<span class="text"><xsl:value-of select="current()"/></span>
</div>
</xsl:for-each>
</div>
The expression "/" selects the document node at the root of the tree containing the context node. (All trees in 1.0 are rooted at document nodes.)
In XSLT 2.0, root() selects the root of the tree containing the context node whether or not the root is a document node.
To get the root of the principal source document even when the context node is a node in a different tree, bind a global variable
<xsl:variable name="principal-root" select="/"/>
which you can refer to anywhere as $principal-root.
Oh, and as Mads Hansen points out, if by "root node" you actually mean the outermost element node, as distinct from the document node, then you would typically use "/*".
The root node is the root of the XML tree, and is the space above the document element. Since an XML document could also have comments and processing instructions as top-level nodes, it gives you the ability to select them as well.
http://www.w3.org/TR/1999/REC-xpath-19991116/#location-paths
/ selects the document root (which is always the parent of the document element)
It sounds as if you want to reference the "root element", also known as the "document element", so that you can get the value of it's #title.
You can select that with the following XPath:
/*/#title
Lets say the user itself is able to add new branch templates.
And on a homepage item, insert options must consist of items inside that branch templates folder.
In sitecore, insert options can only be set to specific items. When I select a folder as an insert option, sitecore shows that folder item (which is perfectly normal).
I need to make something like either showing items dynamically inside a specific folder, or setting the starting path of insert options browsing dialog.
Is any of this possible ?
Blog post: https://sitecorealekseyshevchenko.wordpress.com/2017/09/19/dynamic-insert-options/
Create 'Dynamic Insert Option' template wich contains the only field 'Starting Path' type of 'Droptree' and source value is '{3C1715FE-6A13-4FCF-845F-DE308BA9741D}' - id of '/sitecore/templates' item.
Then add 'Dynamic Insert Option' template to list of templates in 'Base template' field of the template which should have dynamic insert options.
Patch 'uiGetMasters' processor with such config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<processors>
<uiGetMasters>
<processor mode="on"
type="DynamicInsertOption.Processors.GetDynamicInsertOption, DynamicInsertOption"
patch:before="processor[#type='Sitecore.Pipelines.GetMasters.CheckSecurity, Sitecore.Kernel']" />
</uiGetMasters>
</processors>
</sitecore>
</configuration>
Implement GetDynamicInsertOption processor:
namespace DynamicInsertOption.Processors
{
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.GetMasters;
public class GetDynamicInsertOption
{
public void Process(GetMastersArgs args)
{
Assert.ArgumentNotNull(args, "args");
var startingPath = args.Item["Starting Path"];
if (!string.IsNullOrEmpty(startingPath))
{
for (int i = args.Masters.Count - 1; i > -1; i--) { args.Masters.RemoveAt(i); }
var startingFolder = args.Item.Database.GetItem(startingPath);
foreach (Item master in startingFolder.Children) { args.Masters.Add(master); }
}
}
}
}
The result see on pic below:
You can use Sitecore Insert Options Rules instead of Sitecore Insert Options.
Find /sitecore/system/Settings/Rules/Insert Options/Rules item and create new item under that node using Insert Options Rule template. Now edit the rule and set it to:
where the item is the Home item or one of its descendants
and where the parent template is Folder
add YOUR_TEMPLATE insert option
There are plenty of other conditions you can use and the rule can be much more complex if you requirements demands it.
You can read more here: Sitecore Insert Options Rules
I'm using lxml as a solution for XML parsing in my application.
I understand that lxml can only replace the immediate child of a parent, but no levels under that child using .replace
Example XML:
<root>
<dc>
<batman alias='dark_knight' />
</dc>
</root>
I have a modified tag in a string like so
<batman alias='not_dark_knight' />
I need some help with replacing the original XML using xpath '/root/dc/batman'.
from lxml import etree
original_xml = "<root><dc><batman alias='dark_knight' /></dc></root>"
modified_tag = "<batman alias='not_dark_knight' />"
x_path = '/root/dc/batman'
original_obj = etree.fromstring(original_xml)
modified_obj = etree.fromstring(modified_tag)
original_obj.replace(original_obj.xpath(x_path)[0], modified_obj)
This throws a ValueError: Element is not a child of this node.
Is there a way i can replace the string nicely? (only using lxml)
Please understand that I would like a solution using lxml library only.
As you already know, you should be calling replace() on the parent of the element you want to replace. You may use .getparent() to dynamically get to the parent:
batman = original_obj.xpath(x_path)[0]
batman.getparent().replace(batman, modified_obj)
I created a "child" element that is used by my custom "parent" element. My child element tag looks like this in my "parent" element:
<child-element fruits="{{fruits}}"></child-element>
fruits is an array in my "parent" element and is not a property of my "child" element. In my "child" element I have the following:
<template is="dom-repeat" items="[[fruits]]">
<paper-card>[[item.flavor]]</paper-card>
</template>
How can I write a unit test for my "child" element to see if the dom-repeat is working? I want to write the test directly on the "child" element without use of "parent". I know how to write assertions against properties of the "child" element, but I don't know how to bind or pass the fruits array that it would normally listen to from the "parent", since the parent is not part of my test. How can I feed a fruits array to my "child" element?
Once I feed it the fruits array, how can I test to see if the corresponding paper-cards were created?
You actually do have a fruits property on your child element. You set it here:
<child-element fruits="{{fruits}}"></child-element>
This says, set child-element.fruits to parent-element.fruits.
The binding here creates an (implicit) setter for fruits so changes can be observed:
<template is="dom-repeat" items="[[fruits]]">
You can initialize your child element using an array literal in markup:
<child-element fruits='[{"flavor": "apple"}, {"flavor": "blueberry"}]'>
Or by setting the property in JavaScript:
myChildEl.fruits = myFruits;
I think you should be able to check the cards that were created by doing something like:
var cards = Polymer.dom(myChildEl.root).querySelectorAll('paper-card');
i m working on django project on windows...my jstree is like this...
root
parent1
parent2
When I click on parent1 I want to add all of its remaining children (nodes and all sub nodes which is a json data) same with parent2 and so on. Basically when i click on parent then only it should get all its childrens