I have the following structure of the XML file:
<body>
<content>
<text type="header">Header 1</text>
</content>
<content>
<text type="text">Text 1</text>
</content>
</body>
I need to get the first one content tag and then using that content element get his content tag sibling in node.js with select function.
Node.js xpath npm package.
What I am trying to do is:
allDocumentString is a string representation of all the XML
file.
const headerContentTag = select('//content/text[#type = 'header']', allDocumentString);
const textContentTag = select('//following-sibling::content', headerContentTagString);
But it does not work.
I need to exactly get the first one content tag and then depending on that tag the last one.
I know I can get the second one tag just without the first one, but I need the first one.
Strictly speaking, what you need is :
var doc = new dom().parseFromString(xml)
var nodes = xpath.select("//body/content[1][./text[#type='header']]/following-sibling::content[last()]", doc)
We first look for the first content element, child of body and which contains a text element with a #header attribute. Then get we get his last following-sibling.
Related
I'm trying to save data from a XML file into a nested dict. In my XML file, shown bellow, I have multiple tags called DOCUMENT and nested to it I have a variable number of tags called LINK. Then, inside the links I have some URLs inside ADDRESS tags
<document>
<description>blah, blah, blah</description>
<link>
<description>Document1</description>
<address>url 1</address>
</link>
<link>
<description>Document23</description>
<address>url 2</address>
</link>
<link>
<description>Document43</description>
<address>url 3</address>
</link>
<regNum>201801289307</regNum>
<order>3</order>
<seqNum>24447778</seqNum>
<codType>6</codType>
<descType>Blah</descType>
</document>
I have created a dict like this:
op = {}
op['doc_dict'] = {"descriDoc":[], "orderDoc":[], "seqNum":[], "codType":[], "descType":[]}
op['doc_dict']['link_dict'] = {"seqNum":[], "linkUrl":[]}
I would like to achieve a DICT where I can match each URL inside the LINK tags to it's parent DOCUMENT using the value inside the seqNum tag
{'doc_dict': {'descriDoc': ["blah, blah, blah"], 'orderDoc': ["4"], 'seqNum': ["24447779"],
'codType': ["6"], 'descType': ["Blah1"],
'link_dict': {'seqNum': ["24447779"], 'linkUrl': ["url 5", "url 7", "url 9"]}}}
Any idea on how to get the above DICT would be great. All my approaches failed.
Cheers,
I have used the List comprehension and solved the question.
def edicao(filename):
op = []
tree = ET.parse(filename) #read in the XML
for item in tree.iter(tag = 'document'):
doc = {}
doc["descriDoc"] = item.find('description').text
doc["orderDoc"] = item.find('order').text
doc["seqNum"] = item.find('seqNum').text
doc["links"] = [{'seqNum':item.find('seqNum').text,
'descricaoDoc':e.find('description').text,
'url':e.find('address').text} for e in item.findall('link')]
op.append(doc)
return op
Cheers,
I wanted to read the text of "TestData.txt" from the below HTML Code. There is huge html code in the original page. Sharing the below content for reference. There are few more input tags on the top to the below mentioned HTML code.
Can you please correct the below step definition code ?
Step Definition
TestObject testObj5 = new TestObject().addProperty("xpath", ConditionType.EQUALS, "//input[#value='TestData.txt']??")
inq = WebUI.getText(testObj5)
HTML Code
<input type="file" name="file_attach_text" accept="text/html" value="TestData.txt" onkeypress="if(chkKey(event)){return false;}" id="attach_text" class="button" title="Opens a dialog box to select files">
You can select the element using the xpath with the name attribute and then get the value like this:
TestObject testObj5 = new TestObject().addProperty("xpath", ConditionType.EQUALS, "//input[#name='file_attach_text']")
inq = WebUI.getAttribute(testObj5, "value")
I'm working with liferay portal 6.2. And I want to get the value of the text in a tag with alloy user interface.
exemple:
<div>
<p> Paragraph </p>
"value"
</div>
the desired result is: value
please help.
AlloyUI, being an extension of YUI3, uses get/set methods to access and manipulate the properties/attributes of the object (YUI3 Node / AlloyUI Node) that is returned when looking up elements from the page.
Some examples can be reviewed in this documentation as well as this documentation.
In general you'll need something unique (i.e. id, css class) to the div in order to fetch only that element. Once you have that element, divNode.get('text') will give you all of the text within the element. There is not a means to easily "skip" the paragraph contents within the div without the value being contained within some other markup. If you have control over the markup and can do this, that would be the best option. Otherwise you are left to using the replace function to strip out the paragraph contents from the text.
<script>
AUI().use('aui-base', function(A) {
var paragraphText = A.one('#myDiv>p').get('text');
var divText = A.one('#myDiv').get('text')
var onlyValue = divText.replace(paragraphText, "").trim()
console.log(onlyValue)
})
</script>
There is no documentation in the API doc for the constructors. I would like to understand the purpose/use cases for SvgElement.tag() .
The SvgElement.tag(String tag) constructor creates a new SvgElement for a corresponding tag value.
For example:
var foo = new SvgElement.tag('view');
print(foo is ViewElement); // prints 'true'
would create a new SvgElement specified by the <view> tag.
This means that the above code is the same as:
var bar = new ViewElement();
print(bar is ViewElement); // prints 'true'
See also the tag constructor from the superclass Element.
Use cases for this constructor are places where you get the value of the tag from text and want to generate a new element of that tag value.
You might get the tag from parsing the DOM, or maybe from a different API. The tag constructor is a way to write DOM code in a "Darty" way (with objects and classes) while being able to work with DOM elements via text.
In many cases, it is preferable to create this Element object instead of say, using innerHtml to set the DOM inside of another Element.
Compare:
var someTagName = 'view';
var someDomNode = query('#id');
// BAD
someDomNode.innerHtml = '<$someTagName> ... </$someTagName>';
// GOOD
var myElement = new SvgElement.tag(someTagName);
someDomNode.append(myElement);
Let say I have an text box in an HTML page as follows.
<DIV style = "display:none;">
<DIV style = "display:inline;">
<INPUT type = "text" style = "display:inline;">
</DIV>
</DIV>
In this case, the text box will not be visible to the user. How can I identify that text is not currently visible to the user.
Dont say that, I should travel up to the parent objects to find out if they are set to not visible. I have bunch of fields to be validated like this and this would reduce the application performance.
Is there any other way to find out as this object is not visible to the user?
Thanks in advance.
If you don't need it to be pure JavaScript I would suggest using jQuery. Using the :visible or :hidden selector will accomplish what you want:
if ( $('yourElement').is(":hidden") ) {
// The element is not visible
}
http://api.jquery.com/visible-selector/
http://api.jquery.com/hidden-selector/
If you need pure JavaScript and you don't want to travel up through every ancestor element, you could try checking the element's offsetWidth and offsetHeight. If the element is hidden because of an ancestor element, they should both be 0. Note: I've always used jQuery for this, so I don't know how reliable this is.
var yourElement = document.getElementById('yourElementsId');
if ( yourElement.offsetWidth == 0 && yourElement.offsetHeight == 0) {
// The element is not visible
}
https://developer.mozilla.org/en-US/docs/DOM/element.offsetWidth
https://developer.mozilla.org/en-US/docs/DOM/element.offsetHeight