Read key value pairings XML file via ant?

318 views Asked by At

I want to read an XML file like below

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="con" path="org.eclipse.jst.server.core.container/com.ibm.ws.st.core.runtimeClasspathProvider/com.ibm.worklight"/>
  <classpathentry kind="con" path="com.worklight.studio.plugin.classpath.SERVER_CONTAINER"/>
  <classpathentry kind="src" path="server/java"/>
  <classpathentry kind="src" path="common"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <classpathentry kind="src" output="adapters/adp1/bin" path="adapters/agent/src"/>
  <classpathentry kind="src" output="adapters/adp2/bin" path="adapters/alerts/src"/>
  <classpathentry kind="src" output="adapters/adp3/bin" path="adapters/billing/src"/>
  <classpathentry kind="src" output="adapters/adp4/bin" path="adapters/client/src"/>
  <classpathentry kind="src" output="adapters/adp5/bin" path="adapters/category/src"/>
</classpath>

I want to read the value of path where kind is "src". I able to get all the path value but not able to imply condition over it. I am using the following code.

<target name="xml">
  <echo>Test For Each</echo>
  <for list="${classpath.classpathentry.path}" param="letter" delimiter=",">
    <sequential>
      <echo message="path :::  @{letter}"/>
    </sequential>
  </for>
</target>   

It is working fine with all the path values but what I should do to get the value of path where kind is "src" ?

1

There are 1 answers

2
Stavr00 On BEST ANSWER

As I stated in the comment, the following XSLT will parse all classpath entries of kind=src and generate a single line path statement.

getclasspath.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/classpath">
        <xsl:text>path=</xsl:text>
        <xsl:for-each select="classpathentry[@kind='src']">
            <xsl:value-of select="@path"/>
            <xsl:text>;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Then the following ant task:

<xslt style='getclasspath.xslt' in='classpath.xml' out='classpath.properties' />