1

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" ?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
kiran
  • 49
  • 8
  • Could you elaborate how you get the ${classpath.classpathentry.path} value? Your question is tagged with xmltask, is it what you have used ? Please include that snippet too – Patrice M. Jun 19 '15 at 13:00
  • Maybe it would be simpler to use the XSLT task to convert to a simple key=value properties file – Stavr00 Jun 19 '15 at 18:41
  • If you are using xmltask, your xpath syntax is probably insufficient, you'd need to use "//classpath/classpathentry[@kind='src']/@path" – Patrice M. Jun 19 '15 at 22:03
  • @PatriceM. I changed class path.classpathentry.path with //classpath/classpathentry[@kind='src']/@path but it is throwing below error. [echo] Test For Each Property "//classpath/classpathentry[@kind='src']/@path" has not been set Property "//classpath/classpathentry[@kind='src']/@path" has not been set [echo] path ::: ${//classpath/classpathentry[@kind='src']/@path} – kiran Jun 22 '15 at 09:33
  • Please add the complete code snippet with XmlTask ? – Patrice M. Jun 22 '15 at 13:21
  • There's also the `antclipse` task part of ant-contrib. Maybe this will achieve what you want. http://ant-contrib.sourceforge.net/tasks/tasks/antclipse_task.html – Stavr00 Jun 23 '15 at 15:02

1 Answers1

2

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' />
Stavr00
  • 3,219
  • 1
  • 16
  • 28
  • I tried to run as you have written but it is throwing error.. [Fatal Error] getclasspath.xslt:3:6: The processing instruction target matching "[xX][mM][lL]" is not allowed. Fatal Error! org.xml.sax.SAXParseException; systemId: file:/Users/admin/Desktop/getclasspath.xslt; lineNumber: 3; columnNumber: 6; The processing instruction target matching "[xX][mM][lL]" is not allowed. Cause: org.xml.sax.SAXParseException; systemId: file:/Users/admin/Desktop/getclasspath.xslt; lineNumber: 3; columnNumber: 6; The processing instruction target matching "[xX][mM][lL]" is not allowed. – kiran Jun 22 '15 at 09:35
  • 1
    Here's some info on your error http://stackoverflow.com/questions/19889132/error-the-processing-instruction-target-matching-xxmmll-is-not-allowed make sure the XML input and XSLT style are clean. – Stavr00 Jun 22 '15 at 13:53