Using Maven or Ant wanted to get the values from an xml file and replace it by its variables using targets/profiles.
properties.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<variables>
<variable id="Title">
<book.01>abc</book.01>
<book.02>def</book.01>
<ebook.03>ghi</book.01>
<ebook.04>klmn</book.01>
</variable>
<variable id="Author">
<book.01>john</book.01>
<book.02>jack</book.01>
<ebook.03>simi</book.01>
<ebook.04>laura</book.01>
</variable>
</variables>
Using Maven or Ant if I select "book.01" as target or profile, I want to replace the values in Template.xml with values of "book.01" from properties.xml
Current Template.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<mbean code="org.jboss.naming.JNDIBindingServiceMgr"
name="abc.jndi:name=JNDIProp">
<attribute name="myprop" serialDataType="jbxb">
<jndi:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">
<jndi:binding
name="Title">
<jndi:value type="java.lang.String">
@book.01@
</jndi:value>
</jndi:binding>
<jndi:binding name="Author">
<jndi:value type="java.lang.String">
@book.01@
</jndi:value>
</jndi:binding>
</jndi:bindings>
</attribute>
</mbean>
<projects>
Expected output is:book.01.xml
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<mbean code="org.jboss.naming.JNDIBindingServiceMgr"
name="abc.jndi:name=JNDIProp">
<attribute name="myprop" serialDataType="jbxb">
<jndi:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">
<jndi:binding
name="Title">
<jndi:value type="java.lang.String">
abc
</jndi:value>
</jndi:binding>
<jndi:binding name="Author">
<jndi:value type="java.lang.String">
john
</jndi:value>
</jndi:binding>
</jndi:bindings>
</attribute>
</mbean>
<projects>
But wanted to know how should i use same template when i want to run the values for "book.02" or "ebook.03" profiles/target.
Note: The maven profile name/ant target name will be matching the variables of template.xml @varaible@
name.
Example: mvn -P book.01
or ant ebook.01
UPDATES:
With the help of @Ken xsl stylesheet file, I have used the xml-maven plugin to transform the template.xml.Using maven-replacer-plugin I was able to change the @book.01@
varaible in template.xml and replace it with the profile name.
Example:
<profile>
<id>book.02</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>true</ignoreMissingFile>
<file>template.xml</file>
<outputFile>
target/template.xml
</outputFile>
<regex>false</regex>
<token>@book.01@</token>
<value>@book.02@</value>
</configuration>
</plugin>
</plugins>
Are you asking for help, or are you asking for people to solve your entire problem for you? It is obvious that you haven't worked with the above data files because neither of them are well-formed. You are asking volunteers to fix your XML before giving you a complete answer. I suggest in the future that you try to solve the problem first and then ask questions. That way you can be directed regarding where you are having a problem in your understanding, and the files that you post will at least be well-formed.
A solution is below, as I suspect other readers may be interested in the approach using
<xsl:analyze-string>
.t:\ftemp>type properties.xml
t:\ftemp>type Template.xml
t:\ftemp>call xslt2 Template.xml properties.xsl
t:\ftemp>type properties.xsl
t:\ftemp>rem Done!
You can use an XSLT transformation to generate XML files. ANT has a built in xslt task.
Example
build.xml
properties.xml
Your original data was not well-formed. This is my assumption of the correct file content:
template1.xsl
This is the stylesheet used as a template for the output file:
Notes:
result.xml
The generated output: