如何使用配置文件或目标使用模板文件从xml属性文件中获取值

时间:2013-08-12 19:37:56

标签: xml maven xslt ant maven-plugin

使用Maven或Ant想要从xml文件中获取值,并使用目标/配置文件将其替换为变量。

properties.xml如下所示:

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

如果我选择“book.01”作为目标或配置文件,则使用Maven或Ant,我想将来自properties.xml的值为“book.01”的Template.xml中的值替换为

当前的Template.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">
                        @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>

预期输出为: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>

但是当我想运行“book.02”或“ebook.03”个人资料/目标的值时,我想知道如何使用相同的模板。

注意:maven配置文件名称/ ant目标名称将匹配template.xml @varaible@名称的变量。 示例:mvn -P book.01ant ebook.01

更新

在@Ken xsl stylesheet文件的帮助下,我使用了xml-maven插件来转换template.xml。使用maven-replacer-plugin我能够在template.xml中更改@book.01@变量并将其替换为配置文件名称。

示例:

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

2 个答案:

答案 0 :(得分:2)

您是在寻求帮助,还是要求人们为您解决整个问题?很明显,您没有使用上述数据文件,因为它们都没有格式良好。在给出完整答案之前,您要求志愿者修复您的XML。我建议您将来尝试先解决问题,然后提出问题。通过这种方式,您可以了解 您在理解中遇到问题,并且您发布的文件至少会格式正确。

下面是一个解决方案,因为我怀疑其他读者可能对使用<xsl:analyze-string>的方法感兴趣。

t:\ ftemp&gt;输入properties.xml

<?xml version="1.0" encoding="UTF-8"?>
<variables>
    <variable id="Title">
      <book.01>abc</book.01>
      <book.02>def</book.02>
      <ebook.03>ghi</ebook.03>
      <ebook.04>klmn</ebook.04>
    </variable>
    <variable id="Author">
      <book.01>john</book.01>
      <book.02>jack</book.02>
      <ebook.03>simi</ebook.03>
      <ebook.04>laura</ebook.04>
    </variable>
</variables>

t:\ ftemp&gt;输入Template.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">
                        @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>

t:\ ftemp&gt;调用xslt2 Template.xml properties.xsl

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

t:\ ftemp&gt;输入properties.xsl

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:key name="props" match="variable/*"
         use="concat(../@id,'&#xd;',name(.))"/>

<xsl:template match="j:value" xmlns:j="urn:jboss:jndi-binding-service:1.0">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:variable name="id" select="../@name"/>
    <xsl:analyze-string select="." regex="@(.*?)@">
      <xsl:matching-substring>
        <xsl:value-of
          select="key('props',concat($id,'&#xd;',regex-group(1)),
                      doc('properties.xml'))"/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

t:\ ftemp&gt; rem完成!

答案 1 :(得分:2)

您可以使用XSLT转换生成XML文件。 ANT内置xslt task

实施例

├── build.xml
├── src
│   └── resources
│       ├── properties.xml
│       └── template1.xsl
└── target
    └── result.xml

的build.xml

<project name="demo" default="transform">

   <property name="resources.dir" location="src/resources"/>
   <property name="build.dir"     location="target"/>

   <target name="transform">
      <xslt style="${resources.dir}/template1.xsl" in="${resources.dir}/properties.xml" out="${build.dir}/result.xml"/>
   </target>

   <target name="clean">
      <delete dir="${build.dir}"/>
   </target>

</project>

properties.xml中

您的原始数据格式不正确。这是我对正确文件内容的假设:

<?xml version="1.0" encoding="UTF-8"?>
<variables>
    <variable id="Title">
      <book.01>abc</book.01>
      <book.02>def</book.02>
      <ebook.03>ghi</ebook.03>
      <ebook.04>klmn</ebook.04>
    </variable>
    <variable id="Author">
      <book.01>john</book.01>
      <book.02>jack</book.02>
      <ebook.03>simi</ebook.03>
      <ebook.04>laura</ebook.04>
    </variable>
</variables>

template1.xsl

这是用作输出文件模板的样式表:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
                xmlns:xalan="http://xml.apache.org/xslt"
                xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">

   <xsl:output method="xml" indent="yes" xalan:indent-amount="4"/>

   <xsl:template match="/">
      <projects>
         <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="abc.jndi:name=JNDIProp">
            <attribute name="myprop" serialDataType="jbxb">
               <jndi:bindings xmlns:jndi="urn:jboss:jndi-binding-service:1.0" >               
                  <xsl:apply-templates select="//variable[@id='Title']/book.01"  mode="title"/>
                  <xsl:apply-templates select="//variable[@id='Author']/book.01" mode="author"/>
               </jndi:bindings> 
            </attribute>
         </mbean>
      </projects>
   </xsl:template>

   <xsl:template match="book.01" mode="title">
      <jndi:binding name="Title">
         <jndi:value type="java.lang.String"><xsl:value-of select="."/></jndi:value>
      </jndi:binding>
   </xsl:template>

   <xsl:template match="book.01" mode="author">
      <jndi:binding name="Author">
         <jndi:value type="java.lang.String"><xsl:value-of select="."/></jndi:value>
      </jndi:binding>
   </xsl:template>

</xsl:stylesheet>

注意:

  • 此示例使用XSL模板模式匹配依赖于不同XPATH搜索条件的不同“book.01”标记。

为result.xml

生成的输出:

<?xml version="1.0" encoding="UTF-8"?>
<projects xmlns:xalan="http://xml.apache.org/xslt" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="abc.jndi:name=JNDIProp">
        <attribute name="myprop" serialDataType="jbxb">
            <jndi:bindings>
                <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>