How to use parameter in Ireport 4.5 when using xpath and xml as Datasource - xpath

Does anyone know how to use a parameter within and xpath in Ireport. I am using 4.5
I have something like this that works:
<fieldDescription><![CDATA[origin/localizedNames/name[#lang = "en"]]]></fieldDescription>
and what i want to do is something like
<fieldDescription><![CDATA[origin/localizedNames/name[#lang = "$P{lang}"]]]></fieldDescription>
Where lang is my parameter... but it does not seem to work and i cant find any example on the net.
thanks in advance,
Dimitri

You can use parameters in XPath expressions.
The sample:
<queryString language="xPath"><![CDATA[/Northwind/Orders[CustomerID='$P{CustomerID}']]]></queryString>
You can find the sample in folder %jasperreports%\demo\samples\xmldatasource from JasperReports distribution package.
You can also read this article about XPath using.

Related

XPath - Extract spectific file name from string

I'm trying to extract just the filename from a javascript link in import.io, eg googlebolver.htm from href="javascript:finpopup('googlebolver.htm',920,620,0)"
I've managed to get to the 'link' (javascript:finpopup('googlebolver.htm',920,620,0)) with the following XPath
//*[text()='GOOGLE.MAPS']/#href
but I would like to get to the actual address on its own.
As I am running the import.io Extracto on multiple urls, I want it to find something like *.htm
I believe this maybe possible by using the substring function, but I don't know how to do it.
The following questions of this site looked promising, but one only works for fixed length stings and the other I don't completely understand and works for only a specific 'word'
Extract value from javascript object in site using xpath and import.io
How to use substring() with Import.io?
Thanks in advance for your help
EDIT: Here is the URL
You can use the XPath functions substring-after and substring-before, to select the text after, say, (' and before ',
in your example, it would be
substring-before(substring-after(//*[text()='GOOGLE.MAPS']/#href,"('"),"',")
Note: I don't know if import.io supports these standard XPath function

Xpath best practices

I have a readonly xml file and I have a set of xpath values.
I need to create a function which would take in the xpath and return the value(s) corresponding to the xpath.
I am a little confused regarding what would be the best way to proceed. The options I am thinking are using the regular XPathDocument/Navigator/Iterator classes or using LINQ to xml.
The function I am trying to implement is:
T GetString(string inputXpath) where T could be bool/string/array etc.
Can someone help?Also, this function is going to be called all across the application, so performance might be a consideration.
Thank you!
-Agent
What you want to write will just return:
XpathNavigator.Evaluate(inputXpath);
Obviously, T must be just... object :)
Read the XpathNavigator.Evaluate() documentation here.

Is it possible to alter a php file using XPath?

I"m unsure about this. Would having PHP ( or I guess any template language like Django's or Mako or whatever ) inside an html file prevent me from making changes to it with XPath?
I'm very new to XPath. I would think that you could not, but as I said, I'm unsure.
Xpath is a query language. You use it to query XML content, not change it.
You can use Xpath in conjunction with other technologies (XSLT is the first one that comes to mind) in order to query you XML and then use the results of these queries to transform your XML.
XPath doesn't change the XML document.
Use XSLT or a any other XPath-hosting language that can produce a new XML document.

XPATH remove attribute

Hi does anyone know hwo to remove an attrbute using xpath. In particular the rel attribute and its text from a link. i.e. <a href='http://google.com' rel='some text'>Link</a> and i want to remove rel='some text'.
There will be multiple links in the html i am parsing.
You can select items using xpath, but that's all it can do - it is a query language.
You need to use XSLT or an XML parser in order to remove attributes/elements.
As pointed out by Oded, Xpath merely identifies XML nodes. To remove/edit XML, you need some additional tooling.
One solution is the Ant-based plugin XMLTask (disclaimer - I wrote this). It provides a simple mechanism to read an XML file, identify parts of that using XPath, and change it (including removing nodes).
e.g.
<remove path="web/servlet/context[#id='redundant']"/>
Have you already tried using Javascript for this If that is applicable in your scenario:-
var allLinks=document.getElementsByTagName("a");
for(i=0;i<allLinks.length;i++)
{
allLinks[i].removeAttribute("rel");
}

is it possible to specify a OR in a Xpath in a XSD?

I want to be able to guarantee uniqueness for two types of elements : MainQuestion and AlternateQuestion.
In the select query for my xsd:key, can I specify something that would do "//MainQuestion or //AlternateQuestion"? Someone told me that something like this existed, but it seems that XSD only supports a subset of the XPath syntax...
You should be able to use | as usual:
//MainQuestion | //AlternateQuestion
the syntax is indeed restricted - it is roughly the same as restrictions for template patterns in XSLT 1.0, but in addition to that it cannot have any filters in path steps. However, | is explicitly listed as supported.
Supporting Pavel's answer that you can use "|" in an XPath in XML Schema.
XML Schema supports a subset of XPath (which I think of as "fake XPath"). What it supports is explicitly stated in the spec. You have to trace through a few sections to find it. This is a link to the exact section:
http://www.w3.org/TR/xmlschema-1/#c-selector-xpath
Do you have tried the or ?
http://w3schools.com/xpath/xpath_operators.asp

Resources