How to pass variable parameter into XPath expression? The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceGet element by attribute value variables (xpath)Variable within XPath?How can I use a variable in root.findall('*/node[@color=“keyword”]' of ElementTree XML APIUsing variable in XPath contains functionWhat is the best way to avoid XPath Injection attack in Java?How to use Python variable in an XPath expression?How to use variables in Velocity XPath expressions?Add variable in XPath in PythonUsing list variables in xpathXpath with variable partXPath: How to select elements based on their value?How to read XML using XPath in JavaXPath - Selecting elements that equal a valueXPath query to get nth instance of an elementGetting attribute using XPathExtract value of attribute node via XPathXPath to select element based on childs child valueXPath to select Element by attribute valueHow to execute XPath one-liners from shell?How to include a line break in an XPath expression?

When speaking, how do you change your mind mid-sentence?

Why I cannot instantiate a class whose constructor is private in a friend class?

Why did Israel vote against lifting the American embargo on Cuba?

Israeli soda type drink

What's the difference between using dependency injection with a container and using a service locator?

Retract an already submitted Recommendation Letter (written for an undergrad student)

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

Determinant of a matrix with 2 equal rows

Does a Draconic Bloodline sorcerer's doubled proficiency bonus for Charisma checks against dragons apply to all dragon types or only the chosen one?

Will I be more secure with my own router behind my ISP's router?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Why did Europeans not widely domesticate foxes?

Is it OK if I do not take the receipt in Germany?

When does Bran Stark remember Jamie pushing him?

What to do with someone that cheated their way though university and a PhD program?

What is the ongoing value of the Kanban board to the developers as opposed to management

Protagonist's race is hidden - should I reveal it?

Is there a possibility to generate a list dynamically in Latex?

Will I lose my paid in full property

Why is water being consumed when my shutoff valve is closed?

Simulate round-robin tournament draw

Was there ever a LEGO store in Miami International Airport?

In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω

What is the numbering system used for the DSN dishes?



How to pass variable parameter into XPath expression?



The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceGet element by attribute value variables (xpath)Variable within XPath?How can I use a variable in root.findall('*/node[@color=“keyword”]' of ElementTree XML APIUsing variable in XPath contains functionWhat is the best way to avoid XPath Injection attack in Java?How to use Python variable in an XPath expression?How to use variables in Velocity XPath expressions?Add variable in XPath in PythonUsing list variables in xpathXpath with variable partXPath: How to select elements based on their value?How to read XML using XPath in JavaXPath - Selecting elements that equal a valueXPath query to get nth instance of an elementGetting attribute using XPathExtract value of attribute node via XPathXPath to select element based on childs child valueXPath to select Element by attribute valueHow to execute XPath one-liners from shell?How to include a line break in an XPath expression?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I want to pass a parameter into an XPath expression.



(//a/b/c[x=?],myParamForXAttribute)


Can I do this with XPath 1.0 ? (I tried string-join but it is not there in XPath 1.0)



Then how can I do this ?



My XML looks like



<a>
<b>
<c>
<x>val1</x>
<y>abc</y>
</c>
<c>
<x>val2</x>
<y>abcd</y>
</c>
</b>
</a>


I want to get <y> element value where x element value is val1



I tried //a/b/c[x='val1']/y but it did not work.









share
























  • If you're using the Axiom library, you should tag your question Java, and expect an answer that includes some Java in it.

    – Charles Duffy
    May 20 '15 at 15:14






  • 1





    You started with one problem (how to use variables in XPath expressions). I've answered that below. Now, you've added another problem (your new XPath, which doesn't use a variable and tries to select a completely different element, isn't working). Add a new question for your new problem, and include sufficient detail and context to reproduce your problem -- you've not done so here.

    – kjhughes
    May 20 '15 at 15:17












  • BTW, why bother with //a/b/c? If you're going to go to all the performance hit of a recursive search (which is silly, but that's what you do when you use // instead of / to start), might as well use //x[.='val1']/../y, and not need to care about a or b.

    – Charles Duffy
    May 20 '15 at 15:53


















2















I want to pass a parameter into an XPath expression.



(//a/b/c[x=?],myParamForXAttribute)


Can I do this with XPath 1.0 ? (I tried string-join but it is not there in XPath 1.0)



Then how can I do this ?



My XML looks like



<a>
<b>
<c>
<x>val1</x>
<y>abc</y>
</c>
<c>
<x>val2</x>
<y>abcd</y>
</c>
</b>
</a>


I want to get <y> element value where x element value is val1



I tried //a/b/c[x='val1']/y but it did not work.









share
























  • If you're using the Axiom library, you should tag your question Java, and expect an answer that includes some Java in it.

    – Charles Duffy
    May 20 '15 at 15:14






  • 1





    You started with one problem (how to use variables in XPath expressions). I've answered that below. Now, you've added another problem (your new XPath, which doesn't use a variable and tries to select a completely different element, isn't working). Add a new question for your new problem, and include sufficient detail and context to reproduce your problem -- you've not done so here.

    – kjhughes
    May 20 '15 at 15:17












  • BTW, why bother with //a/b/c? If you're going to go to all the performance hit of a recursive search (which is silly, but that's what you do when you use // instead of / to start), might as well use //x[.='val1']/../y, and not need to care about a or b.

    – Charles Duffy
    May 20 '15 at 15:53














2












2








2








I want to pass a parameter into an XPath expression.



(//a/b/c[x=?],myParamForXAttribute)


Can I do this with XPath 1.0 ? (I tried string-join but it is not there in XPath 1.0)



Then how can I do this ?



My XML looks like



<a>
<b>
<c>
<x>val1</x>
<y>abc</y>
</c>
<c>
<x>val2</x>
<y>abcd</y>
</c>
</b>
</a>


I want to get <y> element value where x element value is val1



I tried //a/b/c[x='val1']/y but it did not work.









share
















I want to pass a parameter into an XPath expression.



(//a/b/c[x=?],myParamForXAttribute)


Can I do this with XPath 1.0 ? (I tried string-join but it is not there in XPath 1.0)



Then how can I do this ?



My XML looks like



<a>
<b>
<c>
<x>val1</x>
<y>abc</y>
</c>
<c>
<x>val2</x>
<y>abcd</y>
</c>
</b>
</a>


I want to get <y> element value where x element value is val1



I tried //a/b/c[x='val1']/y but it did not work.







xml xpath axiom xpath-1.0





share














share












share



share








edited Apr 28 '16 at 16:39









kjhughes

67.7k1394135




67.7k1394135










asked May 20 '15 at 14:29









user2694734user2694734

141312




141312












  • If you're using the Axiom library, you should tag your question Java, and expect an answer that includes some Java in it.

    – Charles Duffy
    May 20 '15 at 15:14






  • 1





    You started with one problem (how to use variables in XPath expressions). I've answered that below. Now, you've added another problem (your new XPath, which doesn't use a variable and tries to select a completely different element, isn't working). Add a new question for your new problem, and include sufficient detail and context to reproduce your problem -- you've not done so here.

    – kjhughes
    May 20 '15 at 15:17












  • BTW, why bother with //a/b/c? If you're going to go to all the performance hit of a recursive search (which is silly, but that's what you do when you use // instead of / to start), might as well use //x[.='val1']/../y, and not need to care about a or b.

    – Charles Duffy
    May 20 '15 at 15:53


















  • If you're using the Axiom library, you should tag your question Java, and expect an answer that includes some Java in it.

    – Charles Duffy
    May 20 '15 at 15:14






  • 1





    You started with one problem (how to use variables in XPath expressions). I've answered that below. Now, you've added another problem (your new XPath, which doesn't use a variable and tries to select a completely different element, isn't working). Add a new question for your new problem, and include sufficient detail and context to reproduce your problem -- you've not done so here.

    – kjhughes
    May 20 '15 at 15:17












  • BTW, why bother with //a/b/c? If you're going to go to all the performance hit of a recursive search (which is silly, but that's what you do when you use // instead of / to start), might as well use //x[.='val1']/../y, and not need to care about a or b.

    – Charles Duffy
    May 20 '15 at 15:53

















If you're using the Axiom library, you should tag your question Java, and expect an answer that includes some Java in it.

– Charles Duffy
May 20 '15 at 15:14





If you're using the Axiom library, you should tag your question Java, and expect an answer that includes some Java in it.

– Charles Duffy
May 20 '15 at 15:14




1




1





You started with one problem (how to use variables in XPath expressions). I've answered that below. Now, you've added another problem (your new XPath, which doesn't use a variable and tries to select a completely different element, isn't working). Add a new question for your new problem, and include sufficient detail and context to reproduce your problem -- you've not done so here.

– kjhughes
May 20 '15 at 15:17






You started with one problem (how to use variables in XPath expressions). I've answered that below. Now, you've added another problem (your new XPath, which doesn't use a variable and tries to select a completely different element, isn't working). Add a new question for your new problem, and include sufficient detail and context to reproduce your problem -- you've not done so here.

– kjhughes
May 20 '15 at 15:17














BTW, why bother with //a/b/c? If you're going to go to all the performance hit of a recursive search (which is silly, but that's what you do when you use // instead of / to start), might as well use //x[.='val1']/../y, and not need to care about a or b.

– Charles Duffy
May 20 '15 at 15:53






BTW, why bother with //a/b/c? If you're going to go to all the performance hit of a recursive search (which is silly, but that's what you do when you use // instead of / to start), might as well use //x[.='val1']/../y, and not need to care about a or b.

– Charles Duffy
May 20 '15 at 15:53













2 Answers
2






active

oldest

votes


















5














Given that you're using the Axiom XPath library, which in turn uses Jaxen, you'll need to follow the following three steps to do this in a thoroughly robust manner:



  • Create a SimpleVariableContext, and call context.setVariableValue("val", "value1") to assign a value to that variable.

  • On your BaseXPath object, call .setVariableContext() to pass in the context you assigned.

  • Inside your expression, use /a/b/c[x=$val]/y to refer to that value.


Consider the following:



package com.example;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.common.AxiomText;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.om.xpath.DocumentNavigator;
import org.jaxen.*;

import javax.xml.stream.XMLStreamException;

public class Main

public static void main(String[] args) throws XMLStreamException, JaxenException
String xmlPayload="<parent><a><b><c><x>val1</x><y>abc</y></c>" +
"<c><x>val2</x><y>abcd</y></c>" +
"</b></a></parent>";
OMElement xmlOMOBject = AXIOMUtil.stringToOM(xmlPayload);

SimpleVariableContext svc = new SimpleVariableContext();
svc.setVariableValue("val", "val2");

String xpartString = "//c[x=$val]/y/text()";
BaseXPath contextpath = new BaseXPath(xpartString, new DocumentNavigator());
contextpath.setVariableContext(svc);
AxiomText selectedNode = (AxiomText) contextpath.selectSingleNode(xmlOMOBject);
System.out.println(selectedNode.getText());




...which emits as output:



abcd




share

























  • Can you please post code snippet as an example for above answer. String xmlPayload="<parent><a><c>name1</c></a><b>name2</b></parent>"; OMElement xmlOMOBject= AXIOMUtil.stringToOM(xmlPayload);SimpleVariableContext svc=new SimpleVariableContext(); svc.setVariableValue("part1",key1); svc.setVariableValue("part2",key2); String xpartString="$part1$part2"; DocumentNavigator nav=new DocumentNavigator(); BaseXPath contextpath=new BaseXPath(xmlPayload,nav); contextpath.setVariableContext(svc); contextpath.selectSingleNode(xmlOMOBject);

    – user2694734
    May 28 '15 at 14:17












  • above code gives me an exception

    – user2694734
    May 28 '15 at 15:14











  • @user2694734, it'd be easier to start with a fully ready-to-run reproducer. Maybe put it in a gist (gist.github.com), to allow that to be forked by anyone who wants to propose changes?

    – Charles Duffy
    May 28 '15 at 16:59











  • @user2694734, ...while several years later, I have now added a working example.

    – Charles Duffy
    Mar 21 '18 at 13:03


















4














It depends on the language in which you're using XPath.



In XSLT:



 "//a/b/c[x=$myParamForXAttribute]"


Note that, unlike the approach above, the three below are open to XPath injection attacks and should never be used with uncontrolled or untrusted inputs; to avoid this, use a mechanism provided by your language or library to pass in variables out-of-band. [Credit: Charles Duffy]



In C#:



String.Format("//a/b/c[x=0]", myParamForXAttribute);


In Java:



String.format("//a/b/c[x=%s]", myParamForXAttribute);


In Python:



 "//a/b/c[x=]".format(myParamForXAttribute)




share

























  • I edited my question with my xml. Please have a look. x is a child element of c

    – user2694734
    May 20 '15 at 15:01











  • my scenario is pure Xpath. the evaluated xpath is sent to the java programme. So I want that solution in pure xpath expression

    – user2694734
    May 20 '15 at 15:07











  • I updated the question with xpath Im trying

    – user2694734
    May 20 '15 at 15:11






  • 1





    There is no pure XPath technique for variable evaluation; it depends on the hosting language as I've shown.

    – kjhughes
    May 20 '15 at 15:11






  • 1





    Yes, if your XPath library offers facilities to interpret variables in a manner similar to XSLT, by all means, use it. Since OP didn't originally state his language/library, I answered in general. Many beginners expect there to be a pure XPath solution for embedding variables in XPath, and there is not; as such there's value in leaving this general answer in place.

    – kjhughes
    May 20 '15 at 15:26


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














Given that you're using the Axiom XPath library, which in turn uses Jaxen, you'll need to follow the following three steps to do this in a thoroughly robust manner:



  • Create a SimpleVariableContext, and call context.setVariableValue("val", "value1") to assign a value to that variable.

  • On your BaseXPath object, call .setVariableContext() to pass in the context you assigned.

  • Inside your expression, use /a/b/c[x=$val]/y to refer to that value.


Consider the following:



package com.example;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.common.AxiomText;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.om.xpath.DocumentNavigator;
import org.jaxen.*;

import javax.xml.stream.XMLStreamException;

public class Main

public static void main(String[] args) throws XMLStreamException, JaxenException
String xmlPayload="<parent><a><b><c><x>val1</x><y>abc</y></c>" +
"<c><x>val2</x><y>abcd</y></c>" +
"</b></a></parent>";
OMElement xmlOMOBject = AXIOMUtil.stringToOM(xmlPayload);

SimpleVariableContext svc = new SimpleVariableContext();
svc.setVariableValue("val", "val2");

String xpartString = "//c[x=$val]/y/text()";
BaseXPath contextpath = new BaseXPath(xpartString, new DocumentNavigator());
contextpath.setVariableContext(svc);
AxiomText selectedNode = (AxiomText) contextpath.selectSingleNode(xmlOMOBject);
System.out.println(selectedNode.getText());




...which emits as output:



abcd




share

























  • Can you please post code snippet as an example for above answer. String xmlPayload="<parent><a><c>name1</c></a><b>name2</b></parent>"; OMElement xmlOMOBject= AXIOMUtil.stringToOM(xmlPayload);SimpleVariableContext svc=new SimpleVariableContext(); svc.setVariableValue("part1",key1); svc.setVariableValue("part2",key2); String xpartString="$part1$part2"; DocumentNavigator nav=new DocumentNavigator(); BaseXPath contextpath=new BaseXPath(xmlPayload,nav); contextpath.setVariableContext(svc); contextpath.selectSingleNode(xmlOMOBject);

    – user2694734
    May 28 '15 at 14:17












  • above code gives me an exception

    – user2694734
    May 28 '15 at 15:14











  • @user2694734, it'd be easier to start with a fully ready-to-run reproducer. Maybe put it in a gist (gist.github.com), to allow that to be forked by anyone who wants to propose changes?

    – Charles Duffy
    May 28 '15 at 16:59











  • @user2694734, ...while several years later, I have now added a working example.

    – Charles Duffy
    Mar 21 '18 at 13:03















5














Given that you're using the Axiom XPath library, which in turn uses Jaxen, you'll need to follow the following three steps to do this in a thoroughly robust manner:



  • Create a SimpleVariableContext, and call context.setVariableValue("val", "value1") to assign a value to that variable.

  • On your BaseXPath object, call .setVariableContext() to pass in the context you assigned.

  • Inside your expression, use /a/b/c[x=$val]/y to refer to that value.


Consider the following:



package com.example;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.common.AxiomText;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.om.xpath.DocumentNavigator;
import org.jaxen.*;

import javax.xml.stream.XMLStreamException;

public class Main

public static void main(String[] args) throws XMLStreamException, JaxenException
String xmlPayload="<parent><a><b><c><x>val1</x><y>abc</y></c>" +
"<c><x>val2</x><y>abcd</y></c>" +
"</b></a></parent>";
OMElement xmlOMOBject = AXIOMUtil.stringToOM(xmlPayload);

SimpleVariableContext svc = new SimpleVariableContext();
svc.setVariableValue("val", "val2");

String xpartString = "//c[x=$val]/y/text()";
BaseXPath contextpath = new BaseXPath(xpartString, new DocumentNavigator());
contextpath.setVariableContext(svc);
AxiomText selectedNode = (AxiomText) contextpath.selectSingleNode(xmlOMOBject);
System.out.println(selectedNode.getText());




...which emits as output:



abcd




share

























  • Can you please post code snippet as an example for above answer. String xmlPayload="<parent><a><c>name1</c></a><b>name2</b></parent>"; OMElement xmlOMOBject= AXIOMUtil.stringToOM(xmlPayload);SimpleVariableContext svc=new SimpleVariableContext(); svc.setVariableValue("part1",key1); svc.setVariableValue("part2",key2); String xpartString="$part1$part2"; DocumentNavigator nav=new DocumentNavigator(); BaseXPath contextpath=new BaseXPath(xmlPayload,nav); contextpath.setVariableContext(svc); contextpath.selectSingleNode(xmlOMOBject);

    – user2694734
    May 28 '15 at 14:17












  • above code gives me an exception

    – user2694734
    May 28 '15 at 15:14











  • @user2694734, it'd be easier to start with a fully ready-to-run reproducer. Maybe put it in a gist (gist.github.com), to allow that to be forked by anyone who wants to propose changes?

    – Charles Duffy
    May 28 '15 at 16:59











  • @user2694734, ...while several years later, I have now added a working example.

    – Charles Duffy
    Mar 21 '18 at 13:03













5












5








5







Given that you're using the Axiom XPath library, which in turn uses Jaxen, you'll need to follow the following three steps to do this in a thoroughly robust manner:



  • Create a SimpleVariableContext, and call context.setVariableValue("val", "value1") to assign a value to that variable.

  • On your BaseXPath object, call .setVariableContext() to pass in the context you assigned.

  • Inside your expression, use /a/b/c[x=$val]/y to refer to that value.


Consider the following:



package com.example;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.common.AxiomText;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.om.xpath.DocumentNavigator;
import org.jaxen.*;

import javax.xml.stream.XMLStreamException;

public class Main

public static void main(String[] args) throws XMLStreamException, JaxenException
String xmlPayload="<parent><a><b><c><x>val1</x><y>abc</y></c>" +
"<c><x>val2</x><y>abcd</y></c>" +
"</b></a></parent>";
OMElement xmlOMOBject = AXIOMUtil.stringToOM(xmlPayload);

SimpleVariableContext svc = new SimpleVariableContext();
svc.setVariableValue("val", "val2");

String xpartString = "//c[x=$val]/y/text()";
BaseXPath contextpath = new BaseXPath(xpartString, new DocumentNavigator());
contextpath.setVariableContext(svc);
AxiomText selectedNode = (AxiomText) contextpath.selectSingleNode(xmlOMOBject);
System.out.println(selectedNode.getText());




...which emits as output:



abcd




share















Given that you're using the Axiom XPath library, which in turn uses Jaxen, you'll need to follow the following three steps to do this in a thoroughly robust manner:



  • Create a SimpleVariableContext, and call context.setVariableValue("val", "value1") to assign a value to that variable.

  • On your BaseXPath object, call .setVariableContext() to pass in the context you assigned.

  • Inside your expression, use /a/b/c[x=$val]/y to refer to that value.


Consider the following:



package com.example;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.common.AxiomText;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.om.xpath.DocumentNavigator;
import org.jaxen.*;

import javax.xml.stream.XMLStreamException;

public class Main

public static void main(String[] args) throws XMLStreamException, JaxenException
String xmlPayload="<parent><a><b><c><x>val1</x><y>abc</y></c>" +
"<c><x>val2</x><y>abcd</y></c>" +
"</b></a></parent>";
OMElement xmlOMOBject = AXIOMUtil.stringToOM(xmlPayload);

SimpleVariableContext svc = new SimpleVariableContext();
svc.setVariableValue("val", "val2");

String xpartString = "//c[x=$val]/y/text()";
BaseXPath contextpath = new BaseXPath(xpartString, new DocumentNavigator());
contextpath.setVariableContext(svc);
AxiomText selectedNode = (AxiomText) contextpath.selectSingleNode(xmlOMOBject);
System.out.println(selectedNode.getText());




...which emits as output:



abcd





share













share


share








edited Mar 21 '18 at 13:15

























answered May 20 '15 at 15:17









Charles DuffyCharles Duffy

182k28206261




182k28206261












  • Can you please post code snippet as an example for above answer. String xmlPayload="<parent><a><c>name1</c></a><b>name2</b></parent>"; OMElement xmlOMOBject= AXIOMUtil.stringToOM(xmlPayload);SimpleVariableContext svc=new SimpleVariableContext(); svc.setVariableValue("part1",key1); svc.setVariableValue("part2",key2); String xpartString="$part1$part2"; DocumentNavigator nav=new DocumentNavigator(); BaseXPath contextpath=new BaseXPath(xmlPayload,nav); contextpath.setVariableContext(svc); contextpath.selectSingleNode(xmlOMOBject);

    – user2694734
    May 28 '15 at 14:17












  • above code gives me an exception

    – user2694734
    May 28 '15 at 15:14











  • @user2694734, it'd be easier to start with a fully ready-to-run reproducer. Maybe put it in a gist (gist.github.com), to allow that to be forked by anyone who wants to propose changes?

    – Charles Duffy
    May 28 '15 at 16:59











  • @user2694734, ...while several years later, I have now added a working example.

    – Charles Duffy
    Mar 21 '18 at 13:03

















  • Can you please post code snippet as an example for above answer. String xmlPayload="<parent><a><c>name1</c></a><b>name2</b></parent>"; OMElement xmlOMOBject= AXIOMUtil.stringToOM(xmlPayload);SimpleVariableContext svc=new SimpleVariableContext(); svc.setVariableValue("part1",key1); svc.setVariableValue("part2",key2); String xpartString="$part1$part2"; DocumentNavigator nav=new DocumentNavigator(); BaseXPath contextpath=new BaseXPath(xmlPayload,nav); contextpath.setVariableContext(svc); contextpath.selectSingleNode(xmlOMOBject);

    – user2694734
    May 28 '15 at 14:17












  • above code gives me an exception

    – user2694734
    May 28 '15 at 15:14











  • @user2694734, it'd be easier to start with a fully ready-to-run reproducer. Maybe put it in a gist (gist.github.com), to allow that to be forked by anyone who wants to propose changes?

    – Charles Duffy
    May 28 '15 at 16:59











  • @user2694734, ...while several years later, I have now added a working example.

    – Charles Duffy
    Mar 21 '18 at 13:03
















Can you please post code snippet as an example for above answer. String xmlPayload="<parent><a><c>name1</c></a><b>name2</b></parent>"; OMElement xmlOMOBject= AXIOMUtil.stringToOM(xmlPayload);SimpleVariableContext svc=new SimpleVariableContext(); svc.setVariableValue("part1",key1); svc.setVariableValue("part2",key2); String xpartString="$part1$part2"; DocumentNavigator nav=new DocumentNavigator(); BaseXPath contextpath=new BaseXPath(xmlPayload,nav); contextpath.setVariableContext(svc); contextpath.selectSingleNode(xmlOMOBject);

– user2694734
May 28 '15 at 14:17






Can you please post code snippet as an example for above answer. String xmlPayload="<parent><a><c>name1</c></a><b>name2</b></parent>"; OMElement xmlOMOBject= AXIOMUtil.stringToOM(xmlPayload);SimpleVariableContext svc=new SimpleVariableContext(); svc.setVariableValue("part1",key1); svc.setVariableValue("part2",key2); String xpartString="$part1$part2"; DocumentNavigator nav=new DocumentNavigator(); BaseXPath contextpath=new BaseXPath(xmlPayload,nav); contextpath.setVariableContext(svc); contextpath.selectSingleNode(xmlOMOBject);

– user2694734
May 28 '15 at 14:17














above code gives me an exception

– user2694734
May 28 '15 at 15:14





above code gives me an exception

– user2694734
May 28 '15 at 15:14













@user2694734, it'd be easier to start with a fully ready-to-run reproducer. Maybe put it in a gist (gist.github.com), to allow that to be forked by anyone who wants to propose changes?

– Charles Duffy
May 28 '15 at 16:59





@user2694734, it'd be easier to start with a fully ready-to-run reproducer. Maybe put it in a gist (gist.github.com), to allow that to be forked by anyone who wants to propose changes?

– Charles Duffy
May 28 '15 at 16:59













@user2694734, ...while several years later, I have now added a working example.

– Charles Duffy
Mar 21 '18 at 13:03





@user2694734, ...while several years later, I have now added a working example.

– Charles Duffy
Mar 21 '18 at 13:03













4














It depends on the language in which you're using XPath.



In XSLT:



 "//a/b/c[x=$myParamForXAttribute]"


Note that, unlike the approach above, the three below are open to XPath injection attacks and should never be used with uncontrolled or untrusted inputs; to avoid this, use a mechanism provided by your language or library to pass in variables out-of-band. [Credit: Charles Duffy]



In C#:



String.Format("//a/b/c[x=0]", myParamForXAttribute);


In Java:



String.format("//a/b/c[x=%s]", myParamForXAttribute);


In Python:



 "//a/b/c[x=]".format(myParamForXAttribute)




share

























  • I edited my question with my xml. Please have a look. x is a child element of c

    – user2694734
    May 20 '15 at 15:01











  • my scenario is pure Xpath. the evaluated xpath is sent to the java programme. So I want that solution in pure xpath expression

    – user2694734
    May 20 '15 at 15:07











  • I updated the question with xpath Im trying

    – user2694734
    May 20 '15 at 15:11






  • 1





    There is no pure XPath technique for variable evaluation; it depends on the hosting language as I've shown.

    – kjhughes
    May 20 '15 at 15:11






  • 1





    Yes, if your XPath library offers facilities to interpret variables in a manner similar to XSLT, by all means, use it. Since OP didn't originally state his language/library, I answered in general. Many beginners expect there to be a pure XPath solution for embedding variables in XPath, and there is not; as such there's value in leaving this general answer in place.

    – kjhughes
    May 20 '15 at 15:26















4














It depends on the language in which you're using XPath.



In XSLT:



 "//a/b/c[x=$myParamForXAttribute]"


Note that, unlike the approach above, the three below are open to XPath injection attacks and should never be used with uncontrolled or untrusted inputs; to avoid this, use a mechanism provided by your language or library to pass in variables out-of-band. [Credit: Charles Duffy]



In C#:



String.Format("//a/b/c[x=0]", myParamForXAttribute);


In Java:



String.format("//a/b/c[x=%s]", myParamForXAttribute);


In Python:



 "//a/b/c[x=]".format(myParamForXAttribute)




share

























  • I edited my question with my xml. Please have a look. x is a child element of c

    – user2694734
    May 20 '15 at 15:01











  • my scenario is pure Xpath. the evaluated xpath is sent to the java programme. So I want that solution in pure xpath expression

    – user2694734
    May 20 '15 at 15:07











  • I updated the question with xpath Im trying

    – user2694734
    May 20 '15 at 15:11






  • 1





    There is no pure XPath technique for variable evaluation; it depends on the hosting language as I've shown.

    – kjhughes
    May 20 '15 at 15:11






  • 1





    Yes, if your XPath library offers facilities to interpret variables in a manner similar to XSLT, by all means, use it. Since OP didn't originally state his language/library, I answered in general. Many beginners expect there to be a pure XPath solution for embedding variables in XPath, and there is not; as such there's value in leaving this general answer in place.

    – kjhughes
    May 20 '15 at 15:26













4












4








4







It depends on the language in which you're using XPath.



In XSLT:



 "//a/b/c[x=$myParamForXAttribute]"


Note that, unlike the approach above, the three below are open to XPath injection attacks and should never be used with uncontrolled or untrusted inputs; to avoid this, use a mechanism provided by your language or library to pass in variables out-of-band. [Credit: Charles Duffy]



In C#:



String.Format("//a/b/c[x=0]", myParamForXAttribute);


In Java:



String.format("//a/b/c[x=%s]", myParamForXAttribute);


In Python:



 "//a/b/c[x=]".format(myParamForXAttribute)




share















It depends on the language in which you're using XPath.



In XSLT:



 "//a/b/c[x=$myParamForXAttribute]"


Note that, unlike the approach above, the three below are open to XPath injection attacks and should never be used with uncontrolled or untrusted inputs; to avoid this, use a mechanism provided by your language or library to pass in variables out-of-band. [Credit: Charles Duffy]



In C#:



String.Format("//a/b/c[x=0]", myParamForXAttribute);


In Java:



String.format("//a/b/c[x=%s]", myParamForXAttribute);


In Python:



 "//a/b/c[x=]".format(myParamForXAttribute)





share













share


share








edited May 19 '16 at 13:01

























answered May 20 '15 at 14:56









kjhugheskjhughes

67.7k1394135




67.7k1394135












  • I edited my question with my xml. Please have a look. x is a child element of c

    – user2694734
    May 20 '15 at 15:01











  • my scenario is pure Xpath. the evaluated xpath is sent to the java programme. So I want that solution in pure xpath expression

    – user2694734
    May 20 '15 at 15:07











  • I updated the question with xpath Im trying

    – user2694734
    May 20 '15 at 15:11






  • 1





    There is no pure XPath technique for variable evaluation; it depends on the hosting language as I've shown.

    – kjhughes
    May 20 '15 at 15:11






  • 1





    Yes, if your XPath library offers facilities to interpret variables in a manner similar to XSLT, by all means, use it. Since OP didn't originally state his language/library, I answered in general. Many beginners expect there to be a pure XPath solution for embedding variables in XPath, and there is not; as such there's value in leaving this general answer in place.

    – kjhughes
    May 20 '15 at 15:26

















  • I edited my question with my xml. Please have a look. x is a child element of c

    – user2694734
    May 20 '15 at 15:01











  • my scenario is pure Xpath. the evaluated xpath is sent to the java programme. So I want that solution in pure xpath expression

    – user2694734
    May 20 '15 at 15:07











  • I updated the question with xpath Im trying

    – user2694734
    May 20 '15 at 15:11






  • 1





    There is no pure XPath technique for variable evaluation; it depends on the hosting language as I've shown.

    – kjhughes
    May 20 '15 at 15:11






  • 1





    Yes, if your XPath library offers facilities to interpret variables in a manner similar to XSLT, by all means, use it. Since OP didn't originally state his language/library, I answered in general. Many beginners expect there to be a pure XPath solution for embedding variables in XPath, and there is not; as such there's value in leaving this general answer in place.

    – kjhughes
    May 20 '15 at 15:26
















I edited my question with my xml. Please have a look. x is a child element of c

– user2694734
May 20 '15 at 15:01





I edited my question with my xml. Please have a look. x is a child element of c

– user2694734
May 20 '15 at 15:01













my scenario is pure Xpath. the evaluated xpath is sent to the java programme. So I want that solution in pure xpath expression

– user2694734
May 20 '15 at 15:07





my scenario is pure Xpath. the evaluated xpath is sent to the java programme. So I want that solution in pure xpath expression

– user2694734
May 20 '15 at 15:07













I updated the question with xpath Im trying

– user2694734
May 20 '15 at 15:11





I updated the question with xpath Im trying

– user2694734
May 20 '15 at 15:11




1




1





There is no pure XPath technique for variable evaluation; it depends on the hosting language as I've shown.

– kjhughes
May 20 '15 at 15:11





There is no pure XPath technique for variable evaluation; it depends on the hosting language as I've shown.

– kjhughes
May 20 '15 at 15:11




1




1





Yes, if your XPath library offers facilities to interpret variables in a manner similar to XSLT, by all means, use it. Since OP didn't originally state his language/library, I answered in general. Many beginners expect there to be a pure XPath solution for embedding variables in XPath, and there is not; as such there's value in leaving this general answer in place.

– kjhughes
May 20 '15 at 15:26





Yes, if your XPath library offers facilities to interpret variables in a manner similar to XSLT, by all means, use it. Since OP didn't originally state his language/library, I answered in general. Many beginners expect there to be a pure XPath solution for embedding variables in XPath, and there is not; as such there's value in leaving this general answer in place.

– kjhughes
May 20 '15 at 15:26



Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현