Python3 ElementTree not behaving as expected - python-3.x

I have the following XML file
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-model href="../tool/rules/rules.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<block name="test_debug" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://my-specific-ns"
xsi:noNamespaceSchemaLocation="../tool/rules/schema.xsd" simul="true">
</block>
And my Python3 code is the following
XmlRoot = ET.parse('test.xml').getroot() #type: ElementTree
XmlRoot.attrib
and I get the following output
{'name': 'test_debug', '{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation': '../tool/rules/schema.xsd', 'simul': 'true'}
I do not understand why there are two attributes missing.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
and
xmlns="http://my-specific-ns"
I am interested in getting the xmlns attribute. Because its the namespace used by through my XML files and it will make it easier to search for specific paths.
PS I'm using Python 3.7.
/Regards

Related

Xml script: How do we point our own working directory?

<?xml version="1.0" encoding="UTF-8"?>
<attributes>
<attribute name="priority" type="string"/>
<attribute name="hierarchy" type="script"path="$WORK_DIR/pvim2vmgr/python_script/hierarchy.py">
<scriptParam name="subject"/>
<scriptParam name="test_plan"/>
</attribute>
I'm expecting when I set environment cmd in xterm.
setenv WORK_DIR <my_path>
In xml script, it'll recognize my directory ($WORK_DIR).However, it doesn't, is there any cmd for me to implement it?
Purpose of using xml script
I'm using it to obtain information from certain website and generate a csv file. This xml script is running with command line argument.
Reason I want to replace Working directory
When I'm using other person working_dir to generate this xml script, it automatically replace the $WORK_DIR to other person instead of mine.

Upgrading my weld-servlet dependency from 1.1 to 2.2.9 .. common error shows up

I'm getting started with some CDI stuff (coming from a Spring background.) I'm following some war examples online and they typically use an older dependency of weld-servlet such as 1.1.10.Final.
When I use this dependency my sample app works just fine [Tomcat 7]. However, when I try to upgrade to 2.2.9.Final, I end up getting startup error:
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type UserTransaction with qualifiers #Default
This happens only when I upgrade.
Currently in this simple webapp I have a single beans.xml file located under WEB-INF (although I also tried adding an extra one under META-INF as well.)
Any ideas what I might be be doing wrong.
It just seems so odd that it works fine under 1.1 but not 2.2 ?
My beans.xml I've tried defining two ways (not sure which one is better.. I've seen both)
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated"
>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_1.xsd">
</beans>

jaxb binding customization without having xsd

I've a wsdl (I dont have the .xsd file) and I want to generate the classes from it.
Using wsimport I get a tree of classes that is the standard mapping of the webservice schemas itself and of its dependencies.
I obtain something like com->(microsoft,mycompany), org->(apache).
However I need to remap the package com.mycompany and all the classes inside into com.mycompany.test.
So I've tried to use the option of -b of ws import creating a a docbinding.xml that is Schema Customization XML. The content is :
<jxb:bindings version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings node="wsdl:definitions/wsdl:types/xsd:schema[#targetNamespace='http://mycompany.com/test/']">
<jaxb:package name="com.mycompany.test"/>
</jxb:bindings>
</jxb:bindings>
launching wsimport with this syntax :
wsimport -p com.mycompany -b docbinding.xml https://mycompany.com/nicews/test.svc?wsdl
I obtain a initial error that stops the generation of the classes:
[ERROR] XPath error: null
...
How can I fix the binding XML ?
If the types are in seperate XSD files. This is the way to do it.
Create two configuration files.
wsdl.jxb
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
wsdlLocation="https://mycompany.com/nicews/test.svc?wsdl"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
<jaxws:package name="com.mycompany.wsdl"/> <!-- namespace what you want here -->
</jaxws:bindings>
xsds.jxb
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<!-- This is used becuase we don't need to differentiate between absent and nil elements, you may want to differentiate. If so, remove this binding -->
<jaxb:globalBindings generateElementProperty="false">
<xjc:simple />
</jaxb:globalBindings>
<!-- REPEAT THIS SECTION FOR EACH XSD, replacing schemaLocation and package with whatever you want -->
<jaxb:bindings
schemaLocation="http://mycompany.com/someWsdlIncludeLocation?xsd=xsd0"
node="/xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.mycompany.dto.saml" />
</jaxb:schemaBindings>
</jaxb:bindings>
<!-- END SECTION -->
</jaxb:bindings>
Create a batch file in the same directory
rmdir /S /Q build
rmdir /S /Q dist
rmdir /S /Q src
mkdir build
mkdir dist
mkdir src
"%JAVA_HOME%\bin\wsimport.exe" -b wsdl.jxb -b xsds.jxb -s src -d build -keep http://mycompany.com/someWSDLlocation?wsdl
"%java_home%\bin\jar" cf dist/mycompanyClient.jar -C build/ .
"%java_home%\bin\jar" cf dist/mycompanyClient-src.jar -C src/ .
See if that works for you. Make sure to edit the JXB files appropriately for your wsdl/xsd locations, and packages you want.

Applying external JAXB binding file to schema elements imported from WSDL

The XPath expression in my external binding files can't target the elements in my XML schemas which are imported into my WSDL.
Everything runs if I do inline binding customization but I really wanted to have external binding files that way I never accidentally overwrite(refresh) the files containing my customizations.
The start of my binding file:
<jaxb:bindings
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
version="2.1">
<jaxb:bindings schemaLocation="../wsdl/localhost_7001/ExampleSessionBean/ExampleSessionBeanService.wsdl#types?schema1">
<jaxb:bindings node="//xs:schema[#targetNamespace='urn:myExample']">
My WSDL contains:
<types>
<xsd:schema>
<xsd:import namespace="urn:myExample" schemaLocation="http://localhost:7001/ExampleSessionBean/ExampleSessionBeanService?xsd=1"/>
</xsd:schema>
<xsd:schema>
<xsd:import namespace="http://ejbs/" schemaLocation="http://localhost:7001/ExampleSessionBean/ExampleSessionBeanService?xsd=2"/>
</xsd:schema>
</types>
No matter what I do XPath can't find anything in the xsd:import'ed schemas. The error I get is:
[ERROR] XPath evaluation of "//xs:schema[#targetNamespace='urn:myExample']" results in empty target node
I've tried accessing the xs:schema by index number instead of the namespace and that doesn't work either. It seems like my XPath expressions can't reach elements from imported schemas...is there anyway to fix this?
This is a Java SE 7 project being developed under NetBean 7.2. I'm using NetBeans to do all my wsimport stuff if that matters but the command output looks fairly standard for RI/Metro.
EDIT:
I figured out that I can get an external binding file to work if I use SCD. This XPath example doesn't work:
<bindings node="//xsd:schema[#targetNamespace='urn:myExample']">
<bindings node="//xs:complexType[#name='myType']">
<class name="MyClass"/>
</bindings>
</bindings>
But this SCD example does.
<bindings scd="x-schema::tns" xmlns:tns="urn:myExample">
<bindings scd="~tns:myType">
<class name="MyClass"/>
</bindings>
</bindings>
Is this a known thing where XPath doesn't work in xjb files when using wsimport but SCD does?
you should use it like:
<jaxws:bindings node="wsdl:definitions/wsdl:types/xsd:schema[#targetNamespace='http://duke.example.org']">
<jaxb:schemaBindings>
<jaxb:package name="fromwsdl.server"/>
</jaxb:schemaBindings>
</jaxws:bindings>
Be careful with the namespaces
It all is explained here:
https://jax-ws.java.net/nonav/2.1.2/docs/customizations.html
You could compile each of the XML schemas to Java classes individually. Then you can leverage episode files so that the generated classes can be used when you compile schemas that import that XML schema.
Below is an example of how you produce an episode file.
xjc -b binding1.xml -episode common.episode common.xsd
And below is an example of how you consume and episode file. The episode file is just a JAXB external bindings file and therefore is specified using the -b flag.
xjc -d out main.xsd -extension -b common.episode
For More Information
http://blog.bdoughan.com/2011/12/reusing-generated-jaxb-classes.html
For new people, you can simply use two binding files, one applied to wsdl and other applied to the schema by using the -b option of wsdl2java cxf code generation class accepts multiple binding files:
<java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">
<arg value="-d"/>
<arg value="${src}"/>
<arg value="-b"/>
<arg value="${wsdl.home}\jaxws-bindings.xml"/>
<arg value="-b"/>
<arg value="${wsdl.home}\jaxb-bindings.xml"/>
<arg value="${wsdl.home}\YOUR_WSDL.wsdl"/>
<classpath>
<path refid="cxf.classpath"/>
</classpath>
</java>
Content of file 'jaxws-bindings.xml':
<jaxws:bindings wsdlLocation="YOUR_WSDL.wsdl"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
</jaxws:bindings>
Content of 'jaxb-bindings.xml':
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
jaxb:version="2.0">
<jaxb:bindings schemaLocation="ManagePartyCustomerDataManagement_PARTY_G7-IOP_In_1.0.xsd">
<jaxb:bindings node="//xsd:element[#name='eventDate']">
<jaxb:javaType name="java.util.Date"
parseMethod="com.sofrecom.gaia.ebs.provider.utils.jaxb.StringDateAdapter.parseDate"
printMethod="com.sofrecom.gaia.ebs.provider.utils.jaxb.StringDateAdapter.printDate" />
</jaxb:bindings>
Adding this section to my JAXB configuration helped to do away with a similar error:
<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
<jaxws:enableWrapperStyle>true</jaxws:enableWrapperStyle>
<jaxws:enableAsyncMapping>false</jaxws:enableAsyncMapping>
</jaxws:bindings>
Complete configuration:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
<jaxws:enableWrapperStyle>true</jaxws:enableWrapperStyle>
<jaxws:enableAsyncMapping>false</jaxws:enableAsyncMapping>
</jaxws:bindings>
<jaxb:bindings schemaLocation="ContactService.wsdl" node="/wsdl:definitions/wsdl:types/xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="za.org.kuali.kfs.sys.integration.iapi.contactservice"/>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
Credits:
https://stackoverflow.com/a/7890753/315385
https://stackoverflow.com/a/38077750/315385

MSBuild: Permanently Change PropertyGroup Property of a Project

I was hoping to find a way to set a value in my csproj file during my build to a value. Is there a task in MSBuild that I can use to set a property permanently to a value? In the example below, can I set CustomValue = Yes permanently?
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
.....
<CustomValue>XXXX</CustomValue
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids></ProjectTypeGuids>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
</PropertyGroup>
You can use the XmlPoke task to do that. It seems a little odd to be altering projects this way though. Alternatively, you can set up a tiny import file,
<!-- in your main project file, right below the PropertyGroup -->
<Import
Condition="Exists('Custom.props')"
Project="Custom.props"
/>
Then dynamically create this property file, as,
<?xml version="1.0" encoding="utf-8"?>
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0">
<PropertyGroup>
<CustomValue>True</CustomValue>
</PropertyGroup>
</Project>
You can either use XmlPoke on just this .props file, or use WriteLinesToFile to create the entire file. This secondary file wouldn't need to be checked into source control, the condition on the import makes the project functional when the file doesn't exist.
The XmlPoke task would look like this,
<XmlPoke
XmlInputPath="./Custom.props"
Namespaces="<Namespace Prefix='x'
Uri='http://schemas.microsoft.com/developer/msbuild/2003'/>"
Query="//x:PropertyGroup/x:CustomValue/#Value"
Value="True"
/>

Resources