Merging xml elements with nokogiri - nokogiri

I am trying to get the content of an XML::Element type. I have tried the content method, but that gives me the text and I am
not just interested in the text.
The XML element I have has tags, and I don't want to lose that information. For example, Suppose XML::Element myElement has:
<block>
element has other tags like <tag> ... </tag>
<a href ... > </a> ...
<blah> </blah>
etc..
</block>
So I want a method that will return:
element has other tags like <tag> ... </tag>
<a href ... > </a> ...
<blah> </blah>
etc..
Ultimately, I want to use this to actually merge XML elements into
one. If there exists such method already let me know or if you have
a nice approach to merge them effectively. As an example,
I have two blocks
<block>
block 1 has tags like <tag> ... </tag>
<a href ... > </a> ...
<blah> </blah>
etc..
</block>
<block>
block 2 has tags like <tag> ... </tag>
<a href ... > </a> ...
<blah> </blah>
etc..
</block>
and would want to have it all in one block as follows:
<block>
block 1 has tags like <tag> ... </tag>
<a href ... > </a> ...
<blah> </blah>
etc..
block 2 has tags like <tag> ... </tag>
<a href ... > </a> ...
<blah> </blah>
etc..
</block>
Any help appreciated,
Ted

Ah just realized how to get the information inside a two tags
puts xmlelement.inner_html

Related

Get anchor tag value when anchor tag is inside for each loop

<c:forEach items="${customer.iscutomer}">
<div>
<i>${customer.customerNumber}. </i>
<li>
${customer.customerName}
</li>
</div>
</c:forEach>
i don't know how to get customer name in Struts2 when user selects any hyerlink(customer name come from customer object).
You can use Struts anchor tag and add parameter to the URL.
<s:a href="url"><s:param name="customerName">${customer.customerName}</s:param>CustomerName</s:a>
Note, that you can use EL in the body of the tag.
May be this can help you to start:
<c:forEach items="${customer.iscutomer}">
<div>
<i>${customer.customerNumber}. </i>
<li>
<s:a name="foo" namespace="bar">
<s:param name="customerName">${customer.customerName}</s:param>
<s:param name="customerNubmber">${customer.customerNumber}</s:param>
</s:a>
</li>
</div>
Please consider I find that the s:a url does not add parameters to link. Now your generated link will be /foo/bar/action?customerName=joe&customerNumber=1
No in your JS you can have:
$('a').bind('click',function(){
var url = ($(this).attr('href'));
var customerName= getURLParameter(url, 'customerName');
var customerNumber= getURLParameter(url, 'customerNumber');
//calling the ajax function
pop(cat, typ)
});
If you want to have ajax calls please have a look at struts 2 jquery plugin.

Select following-sibling if element unknown

I want to select the first sibling of a tag from an HTML element. Lets say I want to get the <a> that follows this <span>:
<span id="unique"></span>
<a href="#">
This can easily be done with the following Xpath:
//div[#id="unique"]/following-sibling::a
This, however, is very specific in a way that it looks for an <a> tag. How can I make it more generic that it would find any tag as long as it is the first sibling to the element I selected?
Write as below
//span[#id="unique"]/following-sibling::*[1]
Here is a sample HTML :
<div>
<span id="unique"></span>
<p>foo</p>
<p>bar</p>
</div>
XPATH expression:
//span[#id="unique"]/following-sibling::*[1]
output
<p>foo</p>

How to use if, else condition in jsf to display image

I have a condition where I have an enrollment form in which if userid is 0 it should show the dummy image and when I edit user from any update, I check for if userid which is not equal to 0 then display the image corresponding to userid.
I used JSTL inside jsf page. But always it tries to go to else loop for showing image. The functionality is working fine. The only thing is I can't display the dummy image when I visit the page first. Here s my code.:
<c:if test="${'#{user.userId}' == '0'}">
<a href="Images/thumb_02.jpg" target="_blank" ></a>
<img src="Images/thumb_02.jpg" />
</c:if>
<c:otherwise>
<a href="/DisplayBlobExample?userId=#{user.userId}" target="_blank"</a>
<img src="/DisplayBlobExample?userId=#{user.userId}" />
</c:otherwise>
Can I use this JSTL tag or can I do it using jsf?
For those like I who just followed the code by skuntsel and received a cryptic stack trace, allow me to save you some time.
It seems c:if cannot by itself be followed by c:otherwise.
The correct solution is as follows:
<c:choose>
<c:when test="#{some.test}">
<p>some.test is true</p>
</c:when>
<c:otherwise>
<p>some.test is not true</p>
</c:otherwise>
</c:choose>
You can add additional c:when tests in as necessary.
It is illegal to nest EL expressions: you should inline them. Using JSTL is perfectly valid in your situation. Correcting the mistake, you'll make the code working:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jstl/core">
<c:if test="#{not empty user or user.userId eq 0}">
<a href="Images/thumb_02.jpg" target="_blank" ></a>
<img src="Images/thumb_02.jpg" />
</c:if>
<c:if test="#{empty user or user.userId eq 0}">
<img src="/DisplayBlobExample?userId=#{user.userId}" />
</c:if>
</html>
Another solution is to specify all the conditions you want inside an EL of one element. Though it could be heavier and less readable, here it is:
<img src="#{not empty user or user.userId eq 0 ? '/Images/thumb_02.jpg' : '/DisplayBlobExample?userId='}#{not empty user or user.userId eq 0 ? '' : user.userId}" target="_blank"></img>
Instead of using the "c" tags, you could also do the following:
<h:outputLink value="Images/thumb_02.jpg" target="_blank" rendered="#{not empty user or user.userId eq 0}" />
<h:graphicImage value="Images/thumb_02.jpg" rendered="#{not empty user or user.userId eq 0}" />
<h:outputLink value="/DisplayBlobExample?userId=#{user.userId}" target="_blank" rendered="#{not empty user and user.userId neq 0}" />
<h:graphicImage value="/DisplayBlobExample?userId=#{user.userId}" rendered="#{not empty user and user.userId neq 0}"/>
I think that's a little more readable alternative to skuntsel's alternative answer and is utilizing the JSF rendered attribute instead of nesting a ternary operator.
And off the answer, did you possibly mean to put your image in between the anchor tags so the image is clickable?

Display table tag in struts 2

<display:table export="true" id="data" name="${sessionScope.forbesList}"requestURI="">
<a href="#" rel="tooltip" content="<span>
Name:<s:property value="#data.rank" /><br/>
Email: <a><s:property value="#data.name" /></a><br/>
Phone:<s:property value="#data.age" /> </span>"> <display:column property="rank" title="Rank" sortable="true" /></a>
<display:column property="name" title="Name" sortable="true" />
<display:column property="age" title="Age" sortable="true" />
</display:table>
In Struts 2 I'm trying to use <s:property> tag inside <display :column> tag but I can only see the <display:column> values and not the values that I access with <s:property>. How to do it?
#1: Anchor (<A>) Tag does not have any content attribute.
Please refer to the documentation to see which attributes it supports and how to use them.
#2: in HTML, when putting the character " into an attribute of a Tag, you can:
substitute " with ', like in
myAttribute="this is 'right'" or
myAttribute='this is "right" too', or
encode the " with the html entity ", like in
myAttribute="this is "right""
You must encode the < and > too, with < (Less Than) or > (Greater Than).
#3: that said, you could put your HTML inside the Anchor Tag, in the place where it is supposed to be inserted, like :
<a href="#" rel="tooltip">
Name:<s:property value="#data.rank" />
</a>
but, in your case, you are injecting a lot of HTML, including another Anchor, that is not possible. I think you should simply use a div or something similar as a container, like:
<div rel="tooltip" >
<span>
Name:<s:property value="#data.rank" /><br/>
Email: <a><s:property value="#data.name" /></a><br/>
Phone:<s:property value="#data.age" />
</span>
</div>
Resolve this before worrying about DisplayTag
EDIT
#4
Change
<s:property value="#data.rank" />
(and all the other tags) to
<s:property value="#attr.data.rank" />
#attr is the OGNL used to access the pageContext,
if it is available, otherwise it searches request, session and application scopes.

MVC - RouteLink and Image

I'd like this output:
<a href="\Catalog\Flooring">
<img src="http://site.com/dot.jpg" width="100px" height="100px" alt="" />
<span>Some text here</span>
</a>
using a RouteLink similar to:
<%= Html.RouteLink(myFPV.ProductTypeName, "CatalogType", new { controller = "Catalog", action = "Types", group = myFPV.ProductGroupName, type = myFPV.ProductTypeName })%>
I cannot figure out how to add an <img> and <span> (with text) tags inside my <a> tag.
Make sense?
The first parameter of the RouteLink method is for the link text. But unfortunately, it gets encoded automatically, so you cannot send <, > characters to it. (Well, you can. But they'd get encoded.)
Take a look at this page.
Route values are URL encoded automatically. For example, the string “Hello World!” is
encoded to “Hello%20World!”.
Rendering Image Links
Unfortunately, you can’t use the
Html.ActionLink() helper to render an
image link. Because the
Html.ActionLink() helper HTML encodes
its link text automatically, you
cannot pass an tag to this
method and expect the tag to render as
an image.
Instead, you need to use the
Url.Action() helper to generate the
proper link. Here’s how you can
generate a delete link with an image:
<a href="<%= Url.Action("Delete") %>">
<img src="../../Content/Delete.png" alt="Delete" style="border:0px" />
</a>
I suggest use the Url.RouteUrl.
<a href="#Url.RouteUrl("Default", new { action = "Index", controller = "Department", id = 1 })">
<img src="http://site.com/dot.jpg" width="100px" height="100px" alt="" /> </a>
There is a better option to do that. you should try some thing like
<a href="#Url.RouteUrl("index-lang")">
<img src="~/images/logo.png" alt="">
</a>
where the "index-lang" is the route name in route mapping table.

Resources