Get node with highest value in XPath - xpath

I have an XML about PC components and I need to get the most expensive one. So, in that case I would need to get the node with highest value(price).
XML:
<products>
<product>
<name>Motherboard</name>
<price>150</price>
</product>
<product>
<name>CPU</name>
<price>300</price>
</product>
<product>
<name>RAM</name>
<price>45</price>
</product>
</products>
That is what I have tried so far:
This returns 300 which is correct:
max(/products/product/price)
Result:
But when I try to get the most expensive product, the expression I am using to achieve it does not work:
/products/product[max(price)]
Result:
The expected result should be this one:
<product>
<name>CPU</name>
<price>300</price>
</product>
I am using this online tool
What am I doing wrong?

products/product[price= max(/products/product/price)]
will do :)

XPath 1.0 solution :
//product[not(price<//price)]
https://codebeautify.org/Xpath-Tester

Related

How to turn a file into a Nokogiri::XML object?

I have a sample XML file (let's call it example.xml for the sake of this question) and want to turn it into a Nokogiri object.
According to documentation and lots of other online sources, this should work:
xml = Nokogiri::XML(File.read("example.txt"))
But the value of xml.to_xml is only:
"<?xml version=\"1.0\"?>\n"
In other words, it's ignoring the rest of the file. There are many tags afterwards and none of them are in the xml object.
How do I get Nokogiri to get all the tags?
Here's the XML I'm using:
<? xml version="1.0" encoding="UTF-8" ?>
<Document>
<Test>Test</Test>
</Document>
It looks like you are trying to parse an invalid XML doc.
This can be fixed by removing the spaces in the XML declaration:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Test>Test</Test>
</Document>
How I figured this out
By default, when Nokogiri has errors parsing a document it populates an errors array.
xml = Nokogiri::XML(File.read("example.txt"))
p xml.errors
# => [#<Nokogiri::XML::SyntaxError: xmlParsePI : no target name>, #<Nokogiri::XML::SyntaxError: Start tag expected, '<' not found>]
You can also configure Nokogiri to raise an exception of it has parsing errors:
xml = Nokogiri::XML(File.read("example.txt")) do |config|
config.strict
end
Both of these cases show that there were issues parsing the document

Why can't I get a result from an XPath with namespace in the root element? [duplicate]

This question already has answers here:
Nokogiri/Xpath namespace query
(3 answers)
Closed 8 years ago.
This is probably an XML namespace newbie question but I can't figure out how to get an XPath to work with the following trunctated XML with this particular root element:
<?xml version="1.0" encoding="UTF-8"?>
<CreateOrUpdateEventsRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://dhamma.org" version="3-0-0">
<LanguageKey>
<IsoCode>en</IsoCode>
</LanguageKey>
<Publish>
<Value>true</Value>
</Publish>
<Events>
<Event>
<EventKey>
<LocationKey>
<SubDomain>rasmi</SubDomain>
</LocationKey>
<EventId>10DayPDFStdTag</EventId>
</EventKey>
</Event>
</Events>
</LanguageKey>
</CreateOrUpdateEventsRequest>
Using Ruby and Nokogiri (with a just updated libxml2), it works fine with XPath only if I delete all the extra info in the root element, making it:
<CreateOrUpdateEventsRequest>
Otherwise nothing works:
$> #doc.xpath("//CreateOrUpdateEventsRequest") #=> [] with original header, an array of nodes with modified header
$> #doc.xpath("//LanguageKey") #=> [] with the original header, an array of nodes with modified header
$> #doc.xpath("//xmlns:LanguageKey") #=> undefined namespace prefix with the original
How do I address namespaces like this with XPath?
Many thanks for the help.
The answer seems to be that the XML re-declared XMLNS when it should have declared the namespace with a prefix as in xmlns:myns.
From www.w3.org:
The XML specification reserves all names beginning with the letters 'x', 'm', 'l' in any combination of upper- and lower-case for use by the W3C. To date three such names have been given definitions—although these names are not in the XML namespace, they are listed here as a convenience to readers and users:
xml: See http://www.w3.org/TR/xml/#NT-XMLDecl and http://www.w3.org/TR/xml-names/#xmlReserved
xmlns: See http://www.w3.org/TR/xml-names/#ns-decl
xml-stylesheet: See The xml-stylesheet processing instruction
I don't use Nokogiri nor Ruby,
but you need to register a prefix for namespace http://dhamma.org
When I read http://nokogiri.org/tutorials/searching_a_xml_html_document.html
I understand you must do something like
$> #doc.xpath('//dha:LanguageKey', 'dha' => 'http://dhamma.org')
Here's some code to consider. Starting with code to create a Nokogiri::XML::Document:
require 'nokogiri'
XML = <<EOT
<?xml version="1.0" encoding="UTF-8"?>
<CreateOrUpdateEventsRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://dhamma.org" version="3-0-0">
<LanguageKey>
<IsoCode>en</IsoCode>
</LanguageKey>
<Publish>
<Value>true</Value>
</Publish>
<Events>
<Event>
<EventKey>
<LocationKey>
<SubDomain>rasmi</SubDomain>
</LocationKey>
<EventId>10DayPDFStdTag</EventId>
</EventKey>
</Event>
</Events>
</LanguageKey>
</CreateOrUpdateEventsRequest>
EOT
doc = Nokogiri::XML(XML)
Here's the root node's name:
doc.root.name # => "CreateOrUpdateEventsRequest"
The docs say:
When using CSS, if the namespace is called “xmlns”, you can even omit the namespace name.
doc.at('CreateOrUpdateEventsRequest').name # => "CreateOrUpdateEventsRequest"
doc.at('LanguageKey').to_xml # => "<LanguageKey>\n <IsoCode>en</IsoCode>\n </LanguageKey>"
Using XPath, we can specify the default namespace as:
doc.at('//xmlns:LanguageKey').to_xml # => "<LanguageKey>\n <IsoCode>en</IsoCode>\n </LanguageKey>"
Sometimes, if there are a lot of namespaces it makes sense to use collect_namespaces and pass them in:
name_spaces = doc.collect_namespaces # =>
doc.at('//xmlns:LanguageKey', name_spaces).to_xml # => "<LanguageKey>\n <IsoCode>en</IsoCode>\n </LanguageKey>"
You'll need to look through the documentation for Nokogiri::XML::Node for more information on the various methods.
I recommend using CSS selectors for simplicity and readability over XPath, as a first try. I think XPath has more functionality but it makes my eyes bug out sometimes, so I prefer CSS.

XPATH Error : Unable to evaluate expression

I have an xml like this as mentioned below. I am trying to obtain value for Cardnumber using following expression.
XPATH :
paymentService/ns0:submit/ns0:order/ns0:paymentDetails/ns0:VISA-SSL/cardNumber
But it's giving me error. Can any1 guide me on this?
<?xml version="1.0" encoding="UTF-8"?>
<paymentService version="1.0">
<ns0:submit xmlns:ns0="http://www.tibco.com/ns/no_namespace_schema_location/Payment/PaymentProcessors/WorldPay_CC/SharedResources/Schemas/paymentService_v1.dtd">
<ns0:order>
<description>description</description>
<amount value="500" currencyCode="EUR" exponent="2"/>
<ns0:paymentDetails>
<ns0:VISA-SSL>
<cardNumber>00009875083428500</cardNumber>
<expiryDate>
<date month="02" year="2008"/>
</expiryDate>
<cardHolderName>test</cardHolderName>
</ns0:VISA-SSL>
<session shopperIPAddress="192.165.22.35" id=""/>
</ns0:paymentDetails>
<shopper>
<browser>
<acceptHeader>text/html</acceptHeader>
<userAgentHeader>mozilla 5.0</userAgentHeader>
</browser>
</shopper>
</ns0:order>
</ns0:submit>
</paymentService>
Thanks
Your xmlns:ns0 is misplaced, and (think of this way) because ns0 is defined after <ns0:submit> tag, ns0:submit is "undefined" and thus the parse error.
Edit:
If you need to use this XPath in PHP, you'll either have to declare the namespace before use:
<?xml version="1.0" encoding="UTF-8"?>
<paymentService version="1.0" xmlns:ns0="http://www.tibco.com/ns/no_namespace_schema_location/Payment/PaymentProcessors/WorldPay_CC/SharedResources/Schemas/paymentService_v1.dtd">
<ns0:submit>
Or register the namespace before evaluating your XPath (Thanks to #MiMo for pointing out):
$xml->registerXPathNamespace("ns0","http://www.tibco.com/ns/no_namespace_schema_location/Payment/PaymentProcessors/WorldPay_CC/SharedResources/Schemas/paymentService_v1.dtd");
Also add a slash before your XPATH:
/paymentService/ns0:submit/ns0:order/ns0:paymentDetails/ns0:VISA-SSL/cardNumber
Live demo with declaration first or Live demo with namespace registration (both are demonstrated in PHP).
The problem is the node
<paymentService version="1.0">
Since this is not ended you have to comment it or end it properly.
If you comment that try out with this XPATH
/ns0:submit/ns0:order/ns0:paymentDetails/ns0:VISA-SSL/cardNumber
You need to register the namespace before evaluating the XPath:
$xml->registerXPathNamespace('ns0', 'http://www.tibco.com/ns/no_namespace_schema_location/Payment/PaymentProcessors/WorldPay_CC/SharedResources/Schemas/paymentService_v1.dtd');
where $xml is a SimpleXMLElement variable containing your XML.
Your XPath needs to start with / as per Passerby answer:
/paymentService/ns0:submit/ns0:order/ns0:paymentDetails/ns0:VISA-SSL/cardNumber

Is it possible to pass variable to WIX localization file?

I need to use variable in WIX localization file WIXUI_en-us.wxl.
I tried use it like this:
<String Id="Message_SomeVersionAlreadyInstalled" Overridable="yes">A another version of product $(var.InstallationVersionForGUI) is already installed</String>
But it doesn't work. And when I declared property and used it this way:
<String Id="Message_SomeVersionAlreadyInstalled" Overridable="yes">A another version of product [InstallationVersionForGUI] is already installed</String>
doesn't work either.
Where was I wrong?
Thanks for help and your time.
Localization strings are processed at link time, so you can't use $(var) preprocessor variables. Using a [property] reference is supported, as long as the place where the localization string is used supports run-time formatting (e.g., using the Formatted field type).
Your second method should work just fine. This is the same method used by the default .wxl files.
For example, in your .wxl file you would declare your string:
<String Id="Message_Foo">Foo blah blah [Property1]</String>
And in your .wxs file, you declare the property. If you wish, you can declare the property to match a WiX variable (which it sounds like you're trying to do)
<Property Id="Property1">$(var.Property1)</Property>
I was trying to get localization file to use variables. Came across this post:
There are different layers of variables in WiX (candle's preprocessor
variables, Light's WixVariables/localization variables/binder
variables, and MSI's properties). Each have different syntax and are
evaluated at different times:
Candle's preprocessor variables "$(var.VariableName)" are evaluated
when candle runs, and can be set from candle's commandline and from
"" statements. Buildtime environment
properties as well as custom variables can also be accessed similarly
(changing the "var." prefix with other values).
Light's variables accessible from the command-line are the
WixVariables, and accessing them is via the "!(wix.VariableName)"
syntax. To access your variable from your commandline, you would need
to change your String to: This build was prepared on
!(wix.BuildMachine)
If you instead need to have the BuildMachine value exist as an MSI
property at installation time (which is the "[VariableName]" syntax)
you would need to add the following to one of your wxs files in a
fragment that is already linked in:
Now, the environment variable COMPUTERNAME always has held the name of
my build machines in the past, and you can access that this way:
$(env.COMPUTERNAME). So, you can get rid of the commandline addition
to light.exe and change your wxs file like this:
<WixProperty Id="BuildMachine" Value="$(env.COMPUTERNAME)"/>
Preprocessor variables $(var.VariableName) are are processed at link time, so ideally you would use [PropertyName] which would be defined on the main Product element.
The issue sometimes is that property is not yet defined, for instance using the product name on the localization file seems not posible.
This solution was done aiming to only type the product name once given "Super product" as product name:
In case of running through visual studio extension:
Project properties -> Build -> Define variables -> "MyProductName=Super product" (No quotes)
In case of runing from cmd or some other place:
On Light.exe, add -d"MyProductName=Super product"
Into the localization .wxl file:
<String Id="Description" Overridable="yes">Description of !(wix.MyProductName)
to make it more interesting</String>
I have an aditional config file .wxi I include on other files to have some vars, for instance, here i had hardcoded the value but now it's harcoded on the variable definition and I use the given value:
<?xml version="1.0" encoding="utf-8"?>
<Include>
<!-- Define the product name preprocesor variable -->
<?define ProductName="!(wix.ProductNameDefVar)" ?>
<!-- From this point, can use the preprocesor var -->
<?define ProductName_x64="$(var.ProductName) (64bit)" ?>
<?define ProductName_x32="$(var.ProductName) (32bit)" ?>
<?define CompanyDirName = "My company name" ?>
</Include>
Finally, the place where the localization value where the localization text was not interpolating, is like this:
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<!-- Include the config file with the preprocesor var -->
<?include $(sys.CURRENTDIR)\Config.wxi?>
<!-- Main product definition -->
<Product Id="$(var.ProductCode)"
Name="$(var.ProductName)"
Language="!(loc.Language)"
Version="$(var.BuildVersion)"
Manufacturer="!(loc.Company)"
UpgradeCode="$(var.UpgradeCode)">
<!-- Package details -->
<!-- Here, Description was not interpolating -->
<Package InstallerVersion="200"
Compressed="yes"
InstallScope="perMachine"
Platform="$(var.Platform)"
Manufacturer="!(loc.Company)"
Description="!(loc.Description)"
Keywords="!(loc.Keywords)"
Comments="!(loc.Comments)"
Languages="!(loc.Language)"
/>
[...]

Associate an XML-Stylesheet with an XML Document with Nokogiri

Is it possible to associate a stylesheet with with Nokogiri, to create this structure?
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.my-site.com/sitemap.xsl"?>
<root>
...
</root>
OMG, there is so much fail here that I am breaking the unofficial policy of Team Nokogiri and am providing the correct, sane answer to this question:
require "nokogiri"
doc = Nokogiri::XML "<root>foo</root>"
doc.root.add_previous_sibling Nokogiri::XML::ProcessingInstruction.new(doc, "xml-stylesheet", 'type="text/xsl" href="foo.xsl"')
puts doc.to_xml
# => <?xml version="1.0"?>
# <?xml-stylesheet type="text/xsl" href="foo.xsl"?>
# <root>foo</root>
In the future, please ask questions about Nokogiri on the nokogiri-talk mailing list (http://groups.google.com/group/nokogiri-talk), get the correct answer in a timely fashion, and save everyone a little effort.
There is not.
The way I did it:
xml.gsub!("<?xml version=\"1.0\"?>") do |head|
result = head
result << "\n"
result << "<?xml-stylesheet type=\"text/xsl\" href=\"#{stylesheet}\"?>"
end
Cheers.

Resources