我想读取如下所示的XML文件
<?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>
我想读取kind
"src"
path
的值。我能够获取所有路径值,但无法暗示其条件。我正在使用以下代码。
<target name="xml">
<echo>Test For Each</echo>
<for list="${classpath.classpathentry.path}" param="letter" delimiter=",">
<sequential>
<echo message="path ::: @{letter}"/>
</sequential>
</for>
</target>
它适用于所有path
值,但是我应该怎么做才能获得path
的值kind
"src"
?
正如我在注释中所述,以下 XSLT 将解析 kind=src 的所有类路径条目并生成单个行路径语句。
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>
然后执行以下 ant 任务:
<xslt style='getclasspath.xslt' in='classpath.xml' out='classpath.properties' />