I need to remove the attribute tags <types> </types> without any value in an xml file using ant script - regex

I need to remove the attribute tags without any value in an xml file using ant script
Below is the XML I have:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>DoNothingTest</members>
<name>ApexClass</name>
</types>
<types>
</types>
<types>
</types>
<types>
</types>
<types>
</types>
<version>42.0</version>
</Package>
All the tags that does not have a nesting tag or value should be removed from this xml . The output should look like below:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>DoNothingTest</members>
<name>ApexClass</name>
</types>
<version>42.0</version>
</Package>
How can I achieve this using ANT script?
I am using the below ant script to perform the job, however, it trims only one occurence of 'types' tag.
<!--Format the generated XML file -->
<target name="formatXML" depends="generatePackage.xml">
<property name="final-xml.file" value="temp/src/package.xml"/>
<for param="src-xml.absolute-path" list="${final-xml.file}">
<fileset dir="temp/src/" includes="*.xml"/>
<sequential>
<replaceregexp file="${final-xml.file}"
flags="s"
match="\s*(?:<\/([a-z]+)>)(\s*<\1>)"
replace=""/>
</sequential>
</for>
</target>
How to iterate over the xml file and replace all occurences of 'types' tags?

You can invoke an XSLT transformation from Ant using the XSLT ant task. A stylesheet to remove empty <types> elements in namespace http://soap.sforce.com/2006/04/metadata consists of a boilerplate identity rule to copy all elements unchanged, plus the rule
<xsl:template match="x:types[not(*) and not(text()[normalize-space()])]"
xmlns:x="http://soap.sforce.com/2006/04/metadata"/>

Related

Validation String XSLT WSO2

I'm newbie about validation on programming. Like Checking string and checking field input empty or not. Maybe anyone in here can share me about simple sample validation rule about string and checking field to me with XSLT. Thanks
First of all, XSLT is for XML transformations and if you want to do schema validations you should be looking at XSD validations. Having said that below is how you can use XSLT to validate a message and generate a payload.
Let's say you have a Payload like the below.
<Request>
<messages>
<message>
<id>123</id>
<name>Not Empty</name>
</message>
<message>
<id>234</id>
<name></name>
</message>
</messages>
</Request>
In the above payload, you want to check each message and check whether the name is empty or not. Inorder to validate this you can use XSLT. First create a XSLT as a LocalEntry in the local-entries directory. You can use the following content. (You can create your XSLT in the registry as well)
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="LOCAL_XSLT_NullCheck" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<Response>
<!-- Iterating through message elements -->
<xsl:for-each select="//messages/message">
<message>
<messageID><xsl:value-of select="id/text()"/></messageID>
<xsl:choose>
<!-- Check if name is empty -->
<xsl:when test="boolean(name/text())">
<isEmpty>false</isEmpty>
</xsl:when>
<xsl:otherwise>
<isEmpty>true</isEmpty>
</xsl:otherwise>
</xsl:choose>
</message>
</xsl:for-each>
</Response>
</xsl:template>
</xsl:stylesheet>
</localEntry>
Next create a API to consume your request and to validate the payload and then to generate the response.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/xslt" name="TestAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<log level="full">
<property name="Message" value="Incoming Message"/>
</log>
<xslt key="LOCAL_XSLT_NullCheck"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Now once you invoke the API with the aforementioned message. You should see the following response.
<Response xmlns="http://ws.apache.org/ns/synapse">
<message>
<messageID>123</messageID>
<isEmpty>false</isEmpty>
</message>
<message>
<messageID>234</messageID>
<isEmpty>true</isEmpty>
</message>
</Response>
You ca read more on XSLT mediator from here. You can also consider using FastXSLT Mediator as well based on your requirement. Read more on XSLT from here.

XPATH to get distinct set of nodes from XML

I have an XML file as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:bons0="http://DistinctAppSample" xmlns:httpsca="http://www.ibm.com/xmlns/prod/websphere/http/sca/6.1.0" xmlns:mq="http://www.ibm.com/xmlns/prod/websphere/mq/sca/6.0.0" xmlns:smo="http://www.ibm.com/websphere/sibx/smo/v6.0.1" xmlns:tns="http://DistinctAppSample/TestInf" xmlns:tns1="http://www.w3.org/2005/08/addressing" xmlns:tns2="http://www.w3.org/2003/05/soap-envelope" xmlns:tns_1="wsdl.http://DistinctAppSample/TestInf" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
<tns:testOperation>
<input>
<request>
<salary>100</salary>
<newSalary>101</newSalary>
</request>
<request>
<salary>123</salary>
<newSalary>101</newSalary>
</request>
<request>
<salary>101</salary>
<newSalary>123</newSalary>
</request>
<request>
<salary>100</salary>
<newSalary>101</newSalary>
</request>
</input>
</tns:testOperation>
</body>
I am trying to remove the duplicate "request" node where we have same salary and newSalary value (node1 and node4 has same values. So I need to consider only one node). Sample output is follows.
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:bons0="http://DistinctAppSample" xmlns:httpsca="http://www.ibm.com/xmlns/prod/websphere/http/sca/6.1.0" xmlns:mq="http://www.ibm.com/xmlns/prod/websphere/mq/sca/6.0.0" xmlns:smo="http://www.ibm.com/websphere/sibx/smo/v6.0.1" xmlns:tns="http://DistinctAppSample/TestInf" xmlns:tns1="http://www.w3.org/2005/08/addressing" xmlns:tns2="http://www.w3.org/2003/05/soap-envelope" xmlns:tns_1="wsdl.http://DistinctAppSample/TestInf" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
<tns:testOperation>
<input>
<request>
<salary>100</salary>
<newSalary>101</newSalary>
</request>
<request>
<salary>123</salary>
<newSalary>101</newSalary>
</request>
<request>
<salary>101</salary>
<newSalary>123</newSalary>
</request>
</input>
</tns:testOperation>
</body>
I would need to write a XPATH to achieve this Scenario.
I have tried //request[not(salary = preceding::salary and newSalary = preceding::newSalary)] but it is not working as expected. Any suggesition would be much appriciated.
Thanks.

Jmeter Xslt Test Report "Fatal Error! Content is not allowed in prolog."

I have been update my jmeter from 2.9 to 2.13. I usually use ant build and xslt to generate html report. Here is my ant xml script :
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ballot.xsl"?>
<project default="all">
<!-- Define an environment variable pointing to JMETER folder or change this -->
<property environment="env"/>
<!-- <property name="jmeter-home" location="${env.JMETER_DIR}"/>-->
<property name="jmeter-home" location="C:\apache-jmeter-2.13"/>
<!-- ant-jmeter.jar comes with jmeter, be sure this is the release you have -->
<path id="ant.jmeter.classpath">
<pathelement
location="${jmeter-home}/extras/ant-jmeter-1.1.1.jar" />
</path>
<taskdef
name="jmeter"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"
classpathref="ant.jmeter.classpath" />
<target name="clean">
<delete dir="results2"/>
<delete file="jmeter.log"/>
<mkdir dir="results2/jtl"/>
<mkdir dir="results2/html"/>
</target>
<target name="test" depends="clean">
<jmeter
jmeterhome="${jmeter-home}"
resultlogdir="results2/jtl">
<testplans dir="IAM_jmeter" includes="*.jmx"/>
</jmeter>
</target>
<!-- This is not needed for the plugin, but it produces a nice html report
which can be saved usin hudson's archive artifact feature -->
<target name="report" depends="test">
<xslt
basedir="results2/jtl"
destdir="results2/html"
includes="*.jtl"
style="${jmeter-home}/extras/jmeter-results-detail-report_21.xsl"/>
</target>
<target name="all" depends="test, report"/>
</project>
It worked with jmeter 2.9. But when I update to jmeter 2.13, it genereate an error. Here is the error message :
[xslt] Transforming into C:\Users\Administrator\Downloads\workspace\IAM-JmeterTest-Rest\results2\html
[xslt] Processing C:\Users\Administrator\Downloads\workspace\IAM-JmeterTest-Rest\results2\jtl\IAM_Jmeter.jtl to C:\Users\Administrator\Downloads\workspace\IAM-JmeterTest-Rest\results2\html\IAM_Jmeter.html
[xslt] Loading stylesheet C:\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl
[xslt] C:\Users\Administrator\Downloads\workspace\IAM-JmeterTest-Rest\results2\jtl\IAM_Jmeter.jtl:1:1: Fatal Error! Content is not allowed in prolog.
[xslt] Failed to process null
Any idea why?
Thanks for the attention.
My expectation is that this is due to CSV format of your .jtl file, and for successful XSLT transformation it needs to be XML.
You need to "tell" JMeter to store its results in XML format. To do so add the following line to "test" target:
<property name="jmeter.save.saveservice.output_format" value="xml"/>
So the whole target would look as:
<target name="test" depends="clean">
<jmeter
jmeterhome="${jmeter-home}"
resultlogdir="results2/jtl">
<property name="jmeter.save.saveservice.output_format" value="xml"/>
<testplans dir="IAM_jmeter" includes="*.jmx"/>
</jmeter>
</target>
Another option is adding the following line to user.properties file (lives under /bin folder of your ${jmeter-home}
jmeter.save.saveservice.output_format=xml
See Apache JMeter Properties Customization Guide for more information on JMeter Properties and ways of setting and overriding them.

how to generate xml file using libxml2 in c++?

I want to have following xml file:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE people SYSTEM "XMLSpyDTD.dtd">
<report>
<Start>
<date format="ddMMyy" val="Tue Jun 09 2015
" />
<time format="hhmm" val="11:43:06 " />
</Start>
<test name="Testadd" executed="Yes">
<discription>
<![CDATA[Add two numbers]]>
</discription>
<targets>
<target threaded="false">C++</target>
</targets>
</test>
</report>
So, kindly suggest me how may i generate it using libxml2 in c++?

Creating custom magento webservices

I'm having some trouble creating some custom web services in Magento. I'm trying to get the module configured properly and I can't seem to make the web services I've defined in the api.xml file show up under the user role setup in the admin area.
I've defined a custom module in app/etc shown here
ctp_GiftCards.xml:
<?xml version="1.0"?>
<config>
<modules>
<ctp_GiftCards>
<active>true</active>
<codePool>local</codePool>
</ctp_GiftCards>
</modules>
</config>
The module code is located in app/local/ctp/GiftCards/
Here is an example of the etc/api.xml:
<?xml version="1.0"?>
<config>
<api>
<resources>
<GiftCards translate="title" module="ctp_GiftCards">
<title>GiftCard webservices</title>
<acl>GiftCards/GiftCard</acl>
<methods>
<update translate="title" module="ctp_GiftCards">
<title>updates a giftcard account</title>
</update>
</methods>
<faults module="ctp_GiftCards">
<invalid_data>
<code>100</code>
<message>giftcard data invalid</message>
</invalid_data>
<card_pool_error>
<code>101</code>
<message>card pool for entry not updated</message>
</card_pool_error>
<cache_error>
<code>102</code>
<message>cache not reset</message>
</cache_error>
</faults>
</GiftCards>
</resources>
<acl>
<resources>
<GiftCards translate="title" module="ctp_GiftCards">
<title>GiftCards</title>
<sort_order>6</sort_order>
<GiftCard translate="title" module="ctp_GiftCards">
<title>GiftCard</title>
<update translate="title" module="ctp_GiftCards">
<title>Update</title>
</update>
</GiftCard>
</GiftCards>
</resources>
</acl>
</api>
</config>
and the etc/config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<ctp_GiftCards>
<version>0.1.0</version>
</ctp_GiftCards>
</modules>
<global>
<models>
<GiftCard>
<class>CTP_GiftCards_Model</class>
</GiftCard>
</models>
</global>
</config>
Any help would be much appreciated.
--edit--
I'm using mangeto pro 1.10
Don't use capital letter (GiftCards) in the name of xml tag inside the node. Moreover, your module's name contains both underscope (_) and capital letter (ctp_GiftCards) which will lead Magento to misunderstand.