how to lxml xpath pass attribute values using variables - xpath

How do i pass attrbute as variables , when I hard code values I am getting results but not when I assign a variable
<rootdata>
<Config id="1A" cId="1A13" name="Confg 13">
<Assignment _id="id1eF" of_end_="id1588"/>
<Assignment _id="id1F0" of_end_="id1598" />
</Config>
<Config id="2A" cId="2A14" name="Confg 14">
<Assignment _id="id1eF" of_end_="id151"/>
<Assignment _id="id1F0" of_end_="id152" />
</Config>
<Config id="3A" cId="3A15" name="Confg 14">
<Assignment _id="id1eF" of_end_="id153"/>
<Assignment _id="id1F0" of_end_="id154" />
</Config>
<ele id="id1ef" name="name1"/>
<ele id="id1f0 name="name2"/>
</rootdata>
tree = ET.parse('data.xml')
root = tree.getroot()
for config in root.findall("./Config/[#cId='1A13']/Assignment"):
temp_id=config.attrib['_id']
temp_of_end=config.attrib['of_end_']
print(temp_id,tmp_of_end)
when I hardcode the value for cId it works but I want to pass a variable my var
for config in root.findall("./Config/[#cId=myvariable]/Assignment"):

Try to pass variable as below:
myvariable = "1A13"
root.findall("./Config/*[#cId='{}']/Assignment".format(myvariable))

Related

How to set programatically a selector to a checkbox

I need to set a selector to checkboxes that are created programatically, this way:
var cbAll = new CheckBox(Activity);
LinearLayout.LayoutParams llAll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
cbAll.Text = "All zones";
llAll.LeftMargin = 27;
llAll.TopMargin = 24;
cbAll.ButtonDrawable = //what to put here?
My selector (named cb_edit.xml) in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/cb_active" android:state_checked="true" />
<item android:drawable="#drawable/cb_default" android:state_checked="false"/>
</selector>
i don't know what exactly you trying.but if u setting background then try this
cbAll.SetBackgroundResource( Resource.Drawable.cb_edit);
First, add an XML into your drawable folder:
CheckBoxDrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/CheckedImage" />
<item android:state_checked="false" android:drawable="#drawable/UnCheckedImage" />
</selector>
Then declare the checkbox and set the button drawable to it something like this:
Checkbox btnOption = new Checkbox(context);
btnOption.SetButtonDrawable(Resource.Drawable.CheckBoxDrawable);
That should make it work in case it does not revert

How to replace my custom template for product detail page in Magento2?

I need to replace my custom template for product detail page in magento2.
I have followed this link and updated my code but it is not working.
In my module, I have added catalog_product_view.xml and below code.
Mynamespace\Catalog\view\frontend\layout\catalog_product_view.xml
<referenceContainer name="content">
<block class="Mynamepace\Catalog\Block\Product\View\Article" name="product.view.art" template="Mynamespace_Catalog::product\view\article.phtml">
</block>
</referenceContainer>
This is not working. Can anyone suggest what I am missing here?
My Block Code:
Mynamepace\Catalog\Block\Product\View\Article.php
<?php
namespace Mynamespace\Catalog\Block\Product\View;
use Magento\Catalog\Block\Product\AbstractProduct;
class Article extends AbstractProduct
{
public function showPages()
{
return 'Article';
}
}
My phtml - Mynamespace\Catalog\view\frontend\templates\product\view\article.phtml
I am in Article
<?php
My Module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Mynamespace_Catalog" setup_version="1.0.0" schema_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
Please tell me if I have missed something Or at least tell me how to debug this code.
I see a wrong syntax on your layout file Mynamespace\Catalog\view\frontend\layout\catalog_product_view.xml
template="Mynamespace_Catalog::product\view\article.phtml"
Change it to:
template="Mynamespace_Catalog::product/view/article.phtml"

JAXB Moxy getValueByXpath gives null

I want to see if a theme element exists with specified name in the following xml file.
input.xml
<data>
<artifacts>
<document id="efqw4eads">
<name>composite</name>
</document>
<theme id="1">
<name>Terrace</name>
</theme>
<theme id="2">
<name>Garage</name>
</theme>
<theme id="3">
<name>Reception</name>
</theme>
<theme id="4">
<name>Grade II</name>
</theme>
</artifacts>
</data>
I have the following code. return true statement of the method is never executed. answer always contains a null value.
public boolean themeExists(String name, Data data){
String expression = "artifacts/theme[name='"+name+"']/name/text()";
String answer = jaxbContext.getValueByXPath(data, expression, null, String.class);
if(answer == null || answer.equals("")){
return false;
}
return true;
}
This use case isn't currently supported by EclipseLink JAXB (MOXy). I have opened the following enhancement you can use to track our progress:
http://bugs.eclipse.org/413823
There is no <artifacts/> element you're look for in the first axis step. Your XPath expression should be something like
String expression = "data/theme[name='"+name+"']/name/text()";

Use of text() function when using xPath in dom4j

I have inherited an application that parses xml using dom4j and xPath:
The xml being parsed is similar to the following:
<cache>
<content>
<transaction>
<page>
<widget name="PAGE_ID">WRK_REGISTRATION</widget>
<widget name="TRANS_DETAIL_ID">77145</widget>
<widget name="GRD_ERRORS" />
</page>
<page>
<widget name="PAGE_ID">WRK_REGISTRATION</widget>
<widget name="TRANS_DETAIL_ID">77147</widget>
<widget name="GRD_ERRORS" />
</page>
<page>
<widget name="PAGE_ID">WRK_PROCESSING</widget>
<widget name="TRANS_DETAIL_ID">77152</widget>
<widget name="GRD_ERRORS" />
</page>
</transaction>
</content>
</cache>
Individual Nodes are being searched using the following:
String xPathToGridErrorNode = "//cache/content/transaction/page/widget[#name='PAGE_ID'][text()='WRK_DNA_REGISTRATION']/../widget[#name='TRANS_DETAIL_ID'][text()='77147']/../widget[#name='GRD_ERRORS_TEMP']";
org.dom4j.Element root = null;
SAXReader reader = new SAXReader();
Document document = reader.read(new BufferedInputStream(new ByteArrayInputStream(xmlToParse.getBytes())));
root = document.getRootElement();
Node gridNode = root.selectSingleNode(xPathToGridErrorNode);
where xmlToParse is a String of xml similar to the excerpt provided above.
The code is trying to obtain the GRD_ERROR node for the page with the PAGE_ID and TRANS_DETAIL_ID provided in the xPath.
I am seeing an intermittent (~1-2%) failure (returned node is null) of this selectSingleNode request even though the requested node is in the xml being searched.
I know there are some gotchas associated with using text()= in xPath and was wondering if there was a better way to format the xPath string for this type of search.
From your snippets, there is a problem regarding GRD_ERRORS vs. GRD_ERRORS_TMP and WRK_REGISTRATION vs. WRK_DNA_REGISTRATION.
Ignoring that, I would suggest to rewrite
//cache/content/transaction/page
/widget[#name='PAGE_ID'][text()='WRK_DNA_REGISTRATION']
/../widget[#name='TRANS_DETAIL_ID'][text()='77147']
/../widget[#name='GRD_ERRORS_TEMP']
as
//cache/content/transaction/page
[widget[#name='PAGE_ID'][text()='WRK_REGISTRATION']]
[widget[#name='TRANS_DETAIL_ID'][text()='77147']]
/widget[#name='GRD_ERRORS']
Just because it makes the code, in my eyes, easier to read, and expresses what you seem to mean more clearly: “the page element that has children with these conditions, and then take the widget with this #name.” Or, if that is closer to how you think about it,
//cache/content/transaction/page/widget[#name='GRD_ERRORS']
[preceding-sibling::widget[#name='PAGE_ID'][text()='WRK_REGISTRATION']]
[preceding-sibling::widget[#name='TRANS_DETAIL_ID'][text()='77147']]

Smooks XML-XML transformation with multiple insertion points

I am trying to transform an xml from one format to another using smooks. The source xml looks like what is shown below:
<page>
<responsedata>
<header>
...
<ref_no>xyz</ref_no>
</header>
<detail>
<acc_no>x</acc_no>
<ac_ccy>y</ac_ccy>
<avail_bal>z</avail_bal>
</detail>
<detail>
...
</detail>
</responsedata>
</page>
I am trying to tranform the above to something like this:
<detail>
<ref_no>xyz</ref_no>
<accounts>
<account>
<Account_no>x</Account_no>
<Curr>y</Curr>
<Avail_Bal>z</Avail_Bal>
</account>
<account>
...
</account>
</accounts>
</detail>
For this, I used the following smooks configuration:
<ftl:freemarker applyOnElement="responsedata">
<ftl:template><!--<?xml version="1.0" encoding="UTF-8" ?>
<DETAIL>
<Ref_No>????<Ref_No> //how to bring the ref_no here
<Accounts>
<?TEMPLATE-SPLIT-PI?>
</Accounts>
</DETAIL>
--></ftl:template>
</ftl:freemarker>
<ftl:freemarker applyOnElement="detail">
<ftl:template><!--
<Account>
<Account_no>${detail.acc_no}</Account_no>
<Curr>${detail.ac_ccy}</Curr>
<Avail_Bal>${detail.avail_bal}</Avail_Bal>
</Account>
--></ftl:template>
</ftl:freemarker>
Except for the reference number, I am able to transform everything else. Any suggestions on how to accomplish this would be highly appreciated.
Could you use the Smooks javabean capability to bind that value to a Java object (just put in a HashMap) and then access that bean from the Freemarker template?
<resource-config selector="header">
<resource>org.milyn.javabean.BeanPopulator</resource>
<param name="beanId">header</param>
<param name="beanClass">java.util.HashMap</param>
<param name="bindings">
<binding property="refNo" selector="header/ref_no" />
</param>
</resource-config>
<resource-config selector="header">
<resource type="ftl">
<![CDATA[<detail>
<ref-no>${header.refNo}</ref-no>
...
</detail>]]>
</resource>
</resource-config>

Resources