xpath to grab selected elements - xpath

say i have :
<pages>
<author>Me</author>
<copyright>me inc. 2015,</copyright>
<lastUpdate>2/1/1999</lastUpdate>
<Home>--------------------</Home>
<About>--------------------</About>
<Contact>------------------</Contact>
</pages>
i want two different xpath expressions that: 1. grabs all element between pages and home.
that is, author, copyright, lastUpdate and Home.
2.grabs all element between pages and Contact
excluding Home. that is author, copyright, lastUpdated
and About.
any guide please ?

1. //pages/*[(name() = 'Home') or following-sibling::Home]
2. //pages/*[not(name() = 'Home')]

Related

Xpath - how to differentiate nodes without attributes?

I want to extract a value from xml via xpath and I'm struggling a bit. This is the example of xml I have to work with
<data>
<menu>
<date>2017-10-30</date>
<type>S</type>
<name>onion soup</name>
</menu>
<menu>
<date>2017-10-30</date>
<type>L</type>
<name>ham sandwich</name>
</menu>
<menu>
<date>2017-10-31</date>
<type>S</type>
<name>pumpkin soup</name>
</menu>
<menu>
<date>2017-10-31</date>
<type>L</type>
<name>cheese sandwich</name>
</menu>
<menu>
<date>2017-11-1</date>
<type>S</type>
<name>sweet potato soup</name>
</menu>
<menu>
<date>2017-11-1</date>
<type>L</type>
<name>chicken sandwich</name>
</menu>
</data>
The dates and meal names are dynamically changing.
Now I have 2 columns, for Today's soup and Tomorrow's. I know how to link to xml via xpath for today's soup:
/data/menu/name[../type/text() = "S"] or /data/menu[type[text()='S']]/name
But I struggle with tomorrow's as my xml feed doesn't have any attributes to differentiate, types are the same for both dates and date is constantly changing.
Thanks for any help.
Edit:
Thank you for anwering.
I think I described my problem wrong.
I should probably point out that I'm using Xpath build-in feature in one of the local software.
You're right, these lines
/data/menu[type='S' and date='2017-10-31']]/name
are for all the soups, I just wrongly described it by how it behaves on my end, where it gives me just the value of the first one.
/data/menu[type='S' and date='2017-11-01']]/name
will give me Tommorow's soup, but if I want to use output for this value in static column "Tommorow's soup" next to which I want my xpath output it will only be true for one day. What I need is for it to be true also for next days.
I need a line that will give me "tommorow's soup" which is suppose to be Pumpkin soup today, tommorow when the xml updates it would be Sweet Potato soup and day after that it will be some new soup which is going to be updated later with the whole xml.
If I use
/data/menu[type='S' and date='2017-10-30']]/name
it will not show anything tommorow since there won't be a 2017-10-30 because the xml will update and will start with 2017-10-31.
I hope it's clearer now what I'm asking. I know it's still confusing it's kinda hard for me to describe it in English especially since I'm beginner when it comes to Xpath.
How to differentiate elements without attributes? Use other elements...
But first to clear up a wrong assumption:
Now I have 2 columns, for Today's soup and Tomorrow's. I know how to
link to xml via xpath for today's soup:
/data/menu/name[../type/text() = "S"] or
/data/menu[type[text()='S']]/name
Actually, the XPaths that you say will give you today's soups will actually give you all soups regardless of date.
XPath 1.0
XPath 1.0 has no date functions1, so you'll have to pass the current date and tomorrow's date into your XPAth, and you're on your own to test the date element's value as a string:
If today is 2017-10-31, then this XPath will give you the names of today's soups,
/data/menu[type='S' and date='2017-10-31']]/name
and this XPath will give you the names of tomorrow's soups:
/data/menu[type='S' and date='2017-11-1']]/name
1
XPath 2.0 and 3.0's dynamic context includes a current-dateTime() function, but its format is implementation-dependent, which limits its usefulness. You might be able to use date calculations to determine tomorrow's date, but unless you want to be dependent upon an implementation-defined format for current-dateTime(), you'll have to pass today into your XPath at least.

How to Remove Trailing Zeros in XPath?

I have some not very clean 3rd party data that included numbers like:
PRICE
118.0000
99.0000
etc etc
normally would just use:
{price[1]}
but I just get price like $18,000,000.00 where it should be $118
I tried this (just guessing) ...
number{('price[1]')}
but nothing showed up for the price.
I also tried
format-number{(., 'price[1]')}
but that did not work.
Then I read I can use
translate(#Price, ',.', '.')
I tried that as
translate(#price, ',.', '.')
but no price showed
I then tried several variations using the [1] part , I'm only guessing as I'm not really a coder:
{translate(#price[1], ',.', '.')}
{translate(#price[1],',.','.')} ( I clean the spaces out)
then this one
translate(#price[1], ',.', '.')
and it finally showed a price but only as $1.00 where it should have been $1055 or another one should have been $1145 but they all showed $1.00
What can I do, it must be all on one line as it goes in my web based form to be submitted to import the data?
UPDATED:
Here is what I tried to write in the comments:
I tried these below and this is the results.I used your exact examples including the price as in the first 2 examples, then I tried with the "price" code but that is producing a $1 price again.
substring-before(118.00, '.') $11,800.00
substring-before('118.00','.')$11,800.00
substring-before('price[1]','.')$1.00
substring-before(price[1], '.')$1.00
I also tried using the brackets as I would normaly but that produces no price...
{substring-before(price[1], '.')}
{substring-before('price[1]','.')}
{substring-before('118.00','.')}
{substring-before(118.00, '.')}
I have tried to upload a much smaller copy of some of the input document and just changed some private details with "privatedomain" but I have no permission to include the links so they were deleted.
PROGRAMNAME PROGRAMURL CATALOGNAME LASTUPDATED NAME KEYWORDS DESCRIPTION SKU MANUFACTURER MANUFACTURERID UPC ISBN CURRENCY SALEPRICE PRICE RETAILPRICE FROMPRICE BUYURL IMPRESSIONURL IMAGEURL ADVERTISERCATEGORY THIRDPARTYID THIRDPARTYCATEGORY AUTHOR ARTIST TITLE PUBLISHER LABEL FORMAT SPECIAL GIFT PROMOTIONALTEXT STARTDATE ENDDATE OFFLINE ONLINE INSTOCK CONDITION WARRANTY STANDARDSHIPPINGCOST
PrivateName (deleted link) PrivateName - Product Catalog 2015-03-21 23:06:21.558 Ainsley Cuff, Gold $100-$299, cuff, gold, Open Cuff Captivatingly colorful, Kendra Scott’s collection will spruce up a basic sweater and can simultaneously fancy up a dressier cocktail frock. Her pieces have a southern influence, which brings fun, festivity, and charm to the collection’s aesthetic. 14k Gold Plated 2 Inches Wide Malleable kens-00005B Kendra Scott USD 120.0000 (deleted link) (deleted link)/jpeg_1.jpg Bracelets All Jewelry ,Designers,Shop All,All Jewelry,A-Z Designers,Shop by Occasion,Best of PrivateName,Kendra Scott,Office,Everyday,Vacation,Classic,Casual,Byzantine,Black and White,Gold,Cuff,Destination: Morocco,Back in Stock,Kendra Scott,Bracelets yes
PrivateName (deleted link) PrivateName - Product Catalog 2015-03-21 23:06:21.559 Crystal Deco Brooch $100-$299, crystal "Part of the Ben-Amun Evening Collection. Antique silver-plated over brass Clear Swarovski crystals Length 2.5"" NOTES: This product is made-to-order. Please allow up to 2-3 weeks for delivery. Expedited shipping is not available." BAMU-00037P Ben-Amun Bridal USD 195.0000 (deleted link) (deleted link)/jpg_2.jpg Brooches Collections,Shop By,Designers,Brooches,All Jewelry,Bridal,A-Z Designers,Jewelry Trends,Evening,Bridal,Deco,Crystal,Estate,Ben-Amun Bridal,Shop All yes
PrivateName (deleted link) PrivateName - Product Catalog 2015-03-21 23:06:21.559 Gold Teardrop Cutout Earrings $0-$100, gold "Wendy Mink’s jewelry mixes aspects of traditional Eastern jewelry with classic European design principles. Her pieces are carefully handmade with simple yet unexpected combinations of colors, materials, and shapes. Her collection draws inspiration from textiles created by women in India, Nepal, and Tibet—three regions she spent a great deal of time in while holding a position at the World Bank prior to reinventing herself as a jewelry designer. Gold-plated, 18kt Length 2.5"" Width 1.75"" French wire hook NOTES: This item is made to order and may take up to 3 weeks for delivery." wndm-00107E Wendy Mink USD 73.0000
Please let me know if you need anything else:
If you are using XPATH 1.0 you might be able to use substring-before(xpath, expression)
substring-before(118.00, '.')
should give 118 if I'm not mistaking.

Get the value of title using xPath

I am using the following but it does not get me the value of the title
//*[#id="843285"]/td[3]/a[#title]
Elche vs Osasuna
Can someone give me some guidance?
Few ways to find you the element directly, then use #title to get the title attribute. Note that your id is g843285, not 843285.
If it's always <a> tag (otherwiser use * but with lower performance)
//a[#id="g843285"]/#title
//a[contains(#id, "843285")]/#title
//a[contains(#href, "match/843285")]/#title
I assume you don't know the match teams (otherwise you won't need to find out title), so the following won't work, posting here just for references.
//a[text() = 'Elche vs Osasuna']/#title
"Find the element whose ID attribute equals "843285", and return the value of its title attribute"
//*[#id="843285"]/#title
You can do this if you're always dealing with a tags
//a[#id="g843285"]/#title
Otherwise do //*[#id="g843285"]/#title

Removing .00 in configurable attribute price in Magento

The currency I'm using in the Magento site I'm creating is Japanese Yen which does not have decimals in their currency. I've managed to remove it from my product page and cart.
By modifying app/code/local/Mage/Directory/Model/Currency.php inside format function like so:
$locale = Mage::app()->getLocale()->getLocaleCode();
if($locale != 'ja_JP') {
return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
} else {
return $this->formatPrecision($price, 0, $options, $includeContainer, $addBrackets);
}
However, in the dropdown attribute I created, the decimal still shows. Like so:
White - ¥3000.00
Blue - ¥5000.00
In my dropdown attribute, how can I drop .00 at the end of the price? Also, is it possible to drop decimal for the admin without altering the database?
I've tried searching but sadly, Magento does not have a direct feature that would handle this. Or at least I haven't come across said feature.
You can add this piece of code on beginning of formatTxt method in the same class you was editing:
if(Mage::app()->getLocale()->getLocaleCode() == 'ja_JP') {
$options['precision'] = 0;
}
This will change the precision for prices in almost every place inside Magento (like Catalog, Checkout, even Administration).
Check code of free ET_CurrencyManager extension to find a solution. You need to modify more that one funstion in Mage_Directory_Model_Currency. Also you need to modify precision for JS scripts too in Mage_Core_Model_Locale::getJsPriceFormat.
Or just use this extension to avoid core files modifications.
In fact the Zend local format for JA (Japnese) is wrong, see file:-
httpdocs/lib/Zend/Locale/Data/ja.xml
Line: 2953 (or just search for "0.00")
Update to the following:-
<currencyFormats>
<currencyFormatLength>
<currencyFormat>
<pattern>¤#,##0</pattern>
</currencyFormat>
</currencyFormatLength>
</currencyFormats>
This will completely change the YEN display price to have zero decimals within the JA Locale.
A couple of words of warning...
The admin area if viewed in any locale other than ja_JP will allow a
decimal price to be entered, entering a YEN price with a decimal
position of anything other tha ".00" will result in a checkout price
mismatch error.
It appears that Magento will always send prices to
PayPal with two decimal positions and this will cause price mismatch
errors when applying coupon or Sale discounts (Mage 1.6) - I'm
currently working through this issues on this and testing against
1.9 Version.
Okay, so I managed to remove .00 by modifying configurable.js and product.js. It's not a direct solution but a workaround.
For each file, I split the price string removing any decimal points(.) and numbers succeeding it by using the split function of javascript. So I added something like so:
var a = price.split(.);
return a[0];
Using that, all the decimals have been dropped from my prices. Thanks for all your time and help.

Joomla K2 content sort by number of votes

I'm using Joomla 2.5 and K2 2.5.7. I have a category with posts with a different number of votes. In the front end, I need to sort the items of this category by number of votes.
I re-configured standard stars rating systems to simple "Give one vote" system.
I need this for a ranking order page, so it will have items with the largest number of votes on the top.
What I have
I have my MVC template for K2 category. I was wondering, if sorting $this->leading in category.php is the right to go for.
If it is, how can I do it? With var_dump there is variable numOfVotes which carry real number of votes. How can I sort this object by this var?
Thank you very much!
This K2 forum post seems to answer your question. You need to use the mod_content k2 module and use the "sort by" parameter and select highest rating.
If you go to 'modules/mod_k2_content/helper.php' in the ftp, you'll see on line 98, that there it says
$query .= ", (r.rating_sum/r.rating_count) AS rating";
This sorts the data by the highest rating. Now generally this would sort it by the number of votes divided by the number of people who have voted giving a result between 1 and 5. However as everyone in your case gets a vote of 5 - then your average result will always be 5 by that calculation!, I think that you'll have to replace that line with:
$query .= ", r.rating_count AS rating";
i.e. just sorting by the number of people who have voted (N.B. This assumes you're not using a vote down system as well! You haven't mentioned it so I'm assuming not)
Then you should just be able to use the module (selecting the parameter that you desire as normal)
For using the component category option etc. Then the same line of code can be found in 'components/com_k2/models/itemlist.php' on Line 39 which again would need to be edited. Then you could just use the built in parameters as usual!

Resources