xpaths - retrieve text inside attribute and split / preg_replace? - xpath

I have something that looks like this:
<strong class="citizen_level" title="<strong>Experience Level</strong><br/>1,137,935 / 1,140,000">252</strong>
I managed to extract 252 using :
//div[#id='content']/div[#class='citizen_profile_header']/h2/strong
But I also want to extract 1,137,935, but they have put that inside a title attribute, which I should be able to extract by using
//div[#id='content']/div[#class='citizen_profile_header']/h2/strong/#title
Which should output
<strong>Experience Level</strong><br/>1,137,935 / 1,140,000
Is there any reason the above code wouldnt work ?
And can someone help me here, and get this down to 1137935 ? What would be the best way ?
Edit:
$string = "<strong>Experience Level</strong><br/>1,137,935 / 1,140,000";
$tmp = preg_replace('/[^0-9\/]/',"",$string);
$tmp = ltrim($tmp, '//');
$tmp = preg_split("/[\/,]+/", $tmp);
$string = reset($string);
Maybe not the best way, but it seems to work ? But for some reason
//div[#id='content']/div[#class='citizen_profile_header']/h2/strong/#title
doesn't seem to work =/

Related

How to if elseif correctly with current SMARTY code?

I was converting everything from our existing bash scripts to use our new configuration. So after spending an hour, I noticed the bash scripts weren't even used! I noticed this after making changes didn't work. * facepalm*
Anyways, apparently the interface we use is based mainly on SMARTY templates. Now I want to create several rules, but I am not sure on the proper formatting. Maybe someone can jump in and advice me how to format it correctly?
The original piece of code is this:
if (($data['mem'] != $_SESSION['vps'][$data['veid']]['orig']['memory']) && ($data['mem'] > 0))
{
$parameter .= " --kmemsize ".($data['mem']*203636).":".($data['mem']*224000);
$query[] = "memory=".$data['mem'];
}
Now what I want to do is something like this:
$mem < 768
then this should happen:
$parameter .= " --kmemsize ".($data['mem']*2120000).":".($data['mem']*2140368);
$mem < 2048
then this should happen:
$parameter .= " --kmemsize ".($data['mem']*706672).":".($data['mem']*727040);
else:
$parameter .= " --kmemsize ".($data['mem']*203636).":".($data['mem']*224000);
How do I apply that based on the original code above?
Maybe someone can give me a workable example which I can use to apply it to the above code and create all the rules I need.
I am a bit worried, if I use wrong pieces of code, that I will mess up things. This is something I don't want obviously.
Thanks in advance.

how can I apply xss filter on output of laravel views?

I know I can use {{{}}} for escape all html tags from output texts, but I want to escape only unsafe tags not all tags (for example I want to use br tag in the text)
You should definitely implement it by yourself. I'm assuming that the tags you want to escape are probably just <script> and <iframe>, however in my opinion it is more appropriate to remove entirely that content instead of keeping escaped content on your page for no reason.
You could use regex for simple substitution, something like
$html = preg_replace("/<iframe.*?>/", "", $html);
$html = preg_replace("/<script(.*?)>(.*?)<\/script>/", "", $html);
However it's considered bad practice because the perfect regex expression doesn't exist, so you could have a breach in your security.
A better idea would be using the PHP DOMDocument Parser. You can do something like this to remove script tags:
$doc = new DOMDocument();
$doc->loadHTML($html);
$script_tags = $doc->getElementsByTagName('script');
for ($i = 0; $i < $script_tags->length; $i++) {
$script_tags->item($i)->parentNode->removeChild($script_tags->item($i));
}
$clean_html = $doc->saveHTML();

Replace with use of regular expression result

Let's say I've got some text with a couple tags like this:
[twitter:jpunt]
I want to replace those into something like this:
#Jpunt
How could I do this in Ruby? I've been researching regular expressions for a couple of hours, just with a lot of frustration as a result. Anyone?
This should do the job:
initial = "[twitter:jpunt]"
link = initial.gsub(/\[twitter:(\w+)\]/i, '#\1')
It is one line code (click here to test this code) >>
output = input.gsub(/\[([^:]+):([^\]]+)\]/) {
'#' + $2.capitalize + '' }
The above code works with any tag name. If you want just twitter to be allowed, then go with modification:
output = input.gsub(/\[twitter:([^\]]+)\]/) {
'#' + $1.capitalize + '' }

SoapUI property transfer and xpath/xquery number formatting

I have this property-transfer in SoapUI:
declare namespace soapEnv="http://schemas.xmlsoap.org/soap/envelope/";
//soapEnv:Body/LoginResponse/baseSequenceId
and lets say it returns 123456. But I want 123457 (what I get +1)
I tried this:
declare namespace soapEnv="http://schemas.xmlsoap.org/soap/envelope/";
//soapEnv:Body/LoginResponse/baseSequenceId + 1
but I get 123457.0 as a result. I tried some reformatting methods I found, but most possibly I did not use them in the correct way. I am quite new at this stuff.
I also tried this (with xquery):
declare namespace soapEnv="http://schemas.xmlsoap.org/soap/envelope/";
let $x := //soapEnv:Body/LoginResponse/baseSequenceId
return $x
and tried several things with $x but everything I tried ended up with null or InvocationTargetException.
Any help is appreciated !
Thanks a lot for your suggestions, although I couldn't make them work :(
Maybe there is something wrong with my SoapUI because all xpath functions return null..
I made it work with groovy:
groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
loginResponse = groovyUtils.getXmlHolder("Login#Response")
loginResponse.declareNamespace( "soapEnv", "http://schemas.xmlsoap.org/soap/envelope/" )
sessionIdStr = loginResponse.getNodeValue( "//soapEnv:Body/LoginResponse/sessionId" )
baseSequenceIdStr = loginResponse.getNodeValue( "//soapEnv:Body/LoginResponse/baseSequenceId" )
sequenceIdStr = (baseSequenceIdStr.toInteger() + 1).toString()
createRequest = groovyUtils.getXmlHolder("Create#Request")
createRequest.declareNamespace( "soapEnv", "http://schemas.xmlsoap.org/soap/envelope/" )
createRequest.setNodeValue( "//soapEnv:Header/SessionId", sessionIdStr )
createRequest.setNodeValue( "//soapEnv:Header/TransactionId", baseSequenceIdStr )
createRequest.setNodeValue( "//soapEnv:Header/SequenceId", sequenceIdStr )
createRequest.updateProperty()
Note that if the value of //soapEnv:Body/LoginResponse/baseSequenceId + 1 is an integer, XPath should not put in a decimal point when converting it to a string.
But maybe in this case XPath is returning a number, and it's SoapUI that's converting it to a string, and using a decimal point.
I would first try (updated):
string(//soapEnv:Body/LoginResponse/baseSequenceId + 1)
This is to force the conversion to string to happen within XPath, so that SoapUI won't have a chance to do anything funny with a numeric value.
Alternatively, you could try
floor(//soapEnv:Body/LoginResponse/baseSequenceId + 1)
or even
string(floor(...))
If that doesn't work, you could try
substring-before(//soapEnv:Body/LoginResponse/baseSequenceId + 1, '.')
It's not very elegant, but it might work.

php xpath query on and xpath result

Can I use an xpath query on a result already obtained using xpath?
In most hosting languages/environments (like XSLT, XQuery, DOM) you can. Don't know about PHP, but it would be strange if it doesn't allow this.
Of course, the result of the first query must be a node-set, in order for a future "/" operator to be possible/allowed/successful on it.
I have done it in PHP/SimpleXML. The thing that I didn't understand at first is that you're still dealing with the full SimpleXML object, so if you start with "/nodename", you're operating on root. If you start with "nodename" you are starting at the beginning of the result node. Here's my example:
$parsed=simplexml_load_string($XML);
$s = '/ItemSearchResponse/Items/Item';
$items = $parsed->xpath($s);
foreach($items as $item)
{
$s = 'ItemAttributes/Feature';
$features[]=$item->xpath($s);
$s = 'ASIN';
$asins[]=$item->xpath($s);
$s = 'ImageSets/ImageSet[#Category="primary"]';
$primary_img_set=$item->xpath($s);
$s = 'MediumImage/URL';
$medium_image_url[] = $primary_img_set[0]->xpath($s);
}
In PHP, for example, you can run a query with a context, i.e. a given node. So if you have got a DOMNodeList as a result of the first query you can do things like this:
$query1 = '//p';
$query2 = './a'; // do not forget the dot
$node = $xpath->query($query1)->item(0);
$result = $xpath->query($query2, $node);
Of course this is a silly example because it could have been done just in one shot with the correct XPath experssion but I believe it illustrates your question.

Resources