ASP.NET/XML/LINQ: Extract Attribute from Element - asp.net

I have the following in an XML file:
<entry>
...
<link href="http://www.example.com/somelink" />
...
</entry>
I want to extract the "href" attribute. I've been getting my list of elements like so:
Dim productsDoc = XDocument.Parse(productXML.ToString())
Dim feed = From entry In productsDoc...<entry> Select entry
For Each entry In feed
Dim product As New CartItem
...
product._productid = entry.<id>.Value
product._permalink = entry.<link>.Value 'HOW DO I GET THE ATTRIBUTE?
allItems.Add(product)
Next
How do I extract the <link> href attribute?
Thanks!

Hi to get the link you should use the "XML Attribute Axis Property". This allows you to easily access attributes.
You can use it like this:
'this is how you get the href string
product._permalink = entry.<link>.#href
Here's a full working example of usage:
Module Module1
' some products xml to use for this example
Dim productsXml As XElement = <Xml>
<Product>
<Name>Mountain Bike</Name>
<Price>59.99</Price>
<Link href="http://example.com/bike">Click here for a Mountain Bike</Link>
</Product>
<Product>
<Name>Arsenal Football</Name>
<Price>9.99</Price>
<Link href="http://example.com/arsenalfootball">Click here for a Arsenal Football</Link>
</Product>
<Product>
<Name>Formula One Cap</Name>
<Price>14.99</Price>
<Link href="http://example.com/f1cap">Click here for a Formula One Cap</Link>
</Product>
<Product>
<Name>Robin Hood Bow</Name>
<Price>8.99</Price>
<Link href="http://example.com/robinhoodbow">Click here for a Robin Hood Bow</Link>
</Product>
</Xml>
Sub Main()
' get all <Product> elements from the XDocument
Dim products = From product In productsXml...<Product> _
Select product
' go through each product
For Each product In products
' output the value of the <Name> element within product
Console.WriteLine("Product name is {0}", product.<Name>.Value)
' output the value of the <Price> element within product
Console.WriteLine("Product price is {0}", product.<Price>.Value)
' output the value of the <Link> element within product
Console.WriteLine("Product link text is {0}", product.<Link>.Value)
' output the value of the <Link> href attribute within product
Console.WriteLine("Product href is {0}", product.<Link>.#href)
Next
End Sub
End Module

Related

Nextjs Head tag converts & sign to & in meta and link tag

Let's suppose I have this component.
import Head from 'next/head';
export const Index: React.FC = () => {
const myUrl = `https://example.com/books/filter/authorName='DavidJames'&inStock=true`;
return (
<Head>
<link rel='canonical' href={myUrl} />
<meta property='og:url' content={myUrl} />
</Head>
);
};
export default Index;
You can see that the url contains a & sign.
When I click View page source I see that the & sign is converted to &, so the final url looks like this https://example.com/books/filter/authorName='DavidJames'&inStock=true.
But doing so, the link is partially broken, only first filter will be correctly parsed by backend code.
How to solve this problem?
EDIT: I noticed that this issue only happens for development environment.
Have you tried using double quotes instead of backticks on the url?
const myUrl = "https://example.com/books/filter/authorName='DavidJames'&inStock=true"
Maybe also try adding a backslash before the & character like so
const myUrl = `https://example.com/books/filter/authorName='DavidJames'\&inStock=true`

Liferay and Google Tag Manager implementation

where should I add Google Tag Manager code?
From the documentation of GTM I should copy one script into head tag and another one at the start of body tag.
As far as i could research for liferay it should be added to the theme. Where exactly should I add the GTM code, (head and body code)
Any advice would be helpful, thanks! :)
I also had to configure the GTM. Finally I had to edit the Liferay Theme.
I did it by adding theme settings:
<head>
<title> $ {the_title} - $ {company_name} </title>
<meta content = "initial-scale = 1.0, width = device-width" name = "viewport" />
<#liferay_util ["include"] page = top_head_include />
$ {google_tag_manager_header_script}
</head>
<body class = "$ {css_class}">
$ {google_tag_manager_body_script}
...
</body>
where:
<#assign google_tag_manager_header_script = getterUtil.getString (themeDisplay.getThemeSetting ("google.tag.manager.header.script")) />
<#assign google_tag_manager_body_script = getterUtil.getString (themeDisplay.getThemeSetting ("google.tag.manager.body.script")) />
and in the liferay-look-and-feel.xml I added:
<setting key = "google.tag.manager.header.script" value = "" type = "textarea" configurable = "true"> </setting>
<setting key = "google.tag.manager.body.script" value = "" type = "textarea" configurable = "true"> </setting>
you can use by the optimal way like JBaeza said or without Theme Settings if you use the theme only for one site.
In the last case you can paste the head script before < / head > and the body script next to the < body > tag on the portal_normal.ftl
Regards!

Media content tag missing from rss feed generated by Rome 1.0

I am trying to add some media content to my Rome 1.0 generated RSS feed. But the feed that generates is without my media content tags. I've been looking all over the Internet for answers but no site has really been helpful this far. How do I get my media content to show in my rss feed? Here's my code below:
public org.w3c.dom.Document createMrssFeed(List<Article> recentArticles, String category, String descr) throws Exception {
SyndCategory syndCategory = new SyndCategoryImpl();
syndCategory.setName(category);
List<SyndCategory> categories = new ArrayList<>();
categories.add(syndCategory);
feed.setFeedType("rss_2.0");
feed.setTitle("My feed title");
feed.setLink("http://myfeedlink.com");
feed.setDescription(descr);
feed.setCategories(categories);
feed.setPublishedDate(new Date());
feed.setCopyright("Feed copyright"));
List<SyndEntry> items = new ArrayList<SyndEntry>();
SyndEntry item;
SyndContent description;
for (Article article : recentArticles) {
item = new Item();
item.setTitle(article.getTitle());
item.setLink(createLink(article.getLink()));
description = new SyndContentImpl();
description.setType("text/plain");
description.setValue(article.getDescription());
item.setPublishedDate(article.getPublishedDate());
item.setDescription(description);
MediaContent[] contents = new MediaContent[1];
MediaContent image = new MediaContent( new UrlReference("http://me.com/movie2.jpg"));
contents[0] = image;
Metadata md = new Metadata();
Thumbnail[] thumbs = new Thumbnail[2];
thumbs[0] = new Thumbnail(new URI("http://me.com/movie2.jpg"));
thumbs[1] = new Thumbnail(new URI("http://me.com/movie2.jpg"));
md.setThumbnail( thumbs );
image.setMetadata( md );
MediaEntryModuleImpl module = new MediaEntryModuleImpl();
module.setMediaContents(contents);
item.getModules().add(module);
items.add(item);
}
feed.setEntries(items);
SyndFeedOutput output = new SyndFeedOutput();
org.w3c.dom.Document mrssFeed = output.outputW3CDom(feed);
return mrssFeed;
}
Here's what is generated:
<rss xmlns:atom="http://www.w3.org/2005/Atom"xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>My feed title</title>
<link>http://myfeedlink</link>
<description>news,sports</description>
<category>staff article</category>
<copyright>...</copyright>
<lastBuildDate>Sun, 07 Jun 2015 00:36:31 EDT</lastBuildDate>
<pubDate/>
<item>
<description>Item description</description>
<guid>http://www.myitemlink.com</guid>
<link>http://www.myitemlink.com</link>
<pubDate>Thu, 28 May 2015 10:00:34 EDT</pubDate>
<title>My item title</title>
</item>
<item>
<description>Item 2 description</description>
<guid>http://www.myitem2link.com</guid>
<link>http://www.myitem2link.com</link>
<pubDate>Thu, 28 May 2015 10:00:34 EDT</pubDate>
<title>My item 2 title</title>
</item>
</channel>
</rss>
Can you try Rome 1.5.0? Your code seems to work fine with it and the media tags get generated too.

How to set xml:space="preserve" from Flex/Actionscript

I am working with ActionScript 3 and I need to add a simple tag to one of my XML nodes.
I am trying to add xml:space=preserve tag to one of my text nodes.
var tSpan : XML = new XML ( "<tspan xml:space=\"preserve\"></tspan>" );
My problem is, the moment I add the tSpan node to my main XML, the tag is automatically converted to something like this:
<tspan aaa:space="preserve" xmlns:aaa="http://www.w3.org/XML/1998/namespace">.....
Any idea on how to add xml:... values to an XML node with Actionscript?
Thanks in advance.
Simply create a namespace definition for xml: within your node. It would look like this.
var tSpan : XML = new XML ( "<tspan xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xml:space=\"preserve\"></tspan>" );
The xml:space attribute requires the a namespace definition. See this document for more details. http://www.w3.org/XML/1998/namespace

mako template recursion: AttributeError: 'Undefined' object has no attribute 'id'

I've made a template for representing a tree structure. Each node of the tree has an id, a name, a list of children (tree_children) and an expanded property.
I arranged a few nodes in a tree structure and then called the following function with the root node:
def print_tree_info(oCat, iOffset=0):
"""
just for testing purposes. print to console
"""
sOffset = ' '*iOffset
if oCat.expanded:
sButton = '-'
else:
if oCat.tree_children:
sButton = '+'
else:
sButton = '.'
print("{0}{1}{2}".format(sOffset,sButton,oCat.name))
if oCat.expanded:
for oChild in oCat.tree_children:
print_tree_info(oChild,iOffset+1)
it printed
-ROOT_NODE
+major1
.base2
which is great.
Now, passing the same node structure into the render function of a mako template (along with the mako template itself) I get the attribute error.
Here's how I render the template:
template = Template(..........)
html = template.render(category=root_node, item_template=template)
Here's the template
%if category is UNDEFINED:
ERROR
%elif category:
<div class="tree_item" category_id="${category.id}">
%if category.expanded:
<a class="collapse_tree_item" category_id="${category.id}">-</a>
%elif category.tree_children:
<a class="expand_tree_item" category_id="${oCategory.id}">+</a>
%endif
<a class="select_tree_item">${category.name}</a>
%if category.expanded:
%for oChild in category.tree_children:
${item_template.render(category=oChild,item_template=item_template)}
%endfor
%endif
</div>
%endif
<a class="expand_tree_item" category_id="${oCategory.id}">+</a>
should be
<a class="expand_tree_item" category_id="${category.id}">+</a>
Lesson learned: be consistent in your naming conventions.

Resources