in Education by
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 val1 abc val2 abcd I want to get element value where x element value is val1 I tried //a/b/c[x='val1']/y but it did not work. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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="val1abc" + "val2abcd" + ""; 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

Related questions

0 votes
    AAE's "PDF Integration" command allows you to split a .pdf into a separate single file for a selected number ... by the hyphen. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    There is no explanation in python documentation whether parameters are passed by value or reference and why the ... actual reference? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    The expression of m of the m-derived low pass filter is? (a) m=√(1+(fc/fr)^2) (b) m=√(1+( ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    An expression is a JavaScript object that you pass to the compiled template a)False b)True...
asked Dec 11, 2020 in Education by Editorial Staff
0 votes
    I have an IFrame which shows a PowerBI embedded Report that having a world map. Can I pass a parameter to ... specific country. Thanks Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How to pass a parameter to an event handler or callback in REACT?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    In my Java application I want to have a pool of threads (I use ExecutorService for this) that connect ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have an XML file that starts like this: I'll have to open a lot of these files. Each of these ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    What kind of variable you can access in an lambda expression??...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    In my XSLT stylesheet I extract the value of the current month using a month-from-dateTime like this . ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have an XSLT like this: In the second template, which matches nodes in the fooDocument.xml which is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have an XSLT like this: In the second template, which matches nodes in the fooDocument.xml which is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I store many sub records as Map in the same top record, now I need remove some sub records ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
...