如何使用配置文件或目标,以获得XML属性的值文件中使用模板文件 [英] How to use profiles or targets to get the values from xml properties file using a template file

查看:274
本文介绍了如何使用配置文件或目标,以获得XML属性的值文件中使用模板文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Maven或蚂蚁想从XML文件中获取价值和使用对象/轮廓由它的变量替换它。

properties.xml中看起来是这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
<变量>
    <变量ID =标题>
      < book.01> ABC< /book.01>
      < book.02>&高清LT; /book.01>
      < ebook.03>&GHI LT; /book.01>
      < ebook.04> klmn< /book.01>
    < /变量>
    <变量ID =作者>
      < book.01>约翰和LT; /book.01>
      < book.02> JACK< /book.01>
      < ebook.03>西米< /book.01>
      < ebook.04>&劳拉LT; /book.01>
    < /变量>
< /变量>

使用Maven或Ant的,如果我选择book.01作为目标或配置文件,我想用book.01的价值观,以取代在Template.xml中值从properties.xml中

目前Template.xml中看起来是这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
<项目>
    <的MBean code =org.jboss.naming.JNDIBindingServiceMgr
        NAME =abc.jndi:名称= JNDIProp>
        <属性名=mypropserialDataType =jbxb>
            < JNDI:绑定
                的xmlns:XS =htt​​p://www.w3.org/2001/XMLSchema-instance
                的xmlns:JNDI =瓮:JBoss的:JNDI绑定服务:1.0
                XS:的schemaLocation =瓮:JBoss的:JNDI绑定服务:1.0资源:JNDI的绑定service_1_0.xsd>
                < JNDI:结合
                    NAME =标题>
                    < JNDI:值类型=java.lang.String中>
                        @ book.01 @
                    < / JNDI:值>
                < / JNDI:结合>
                < JNDI:绑定名称=作者>
                    < JNDI:值类型=java.lang.String中>
                        @ book.01 @
                    < / JNDI:值>
                < / JNDI:结合>
            < / JNDI:绑定>
        < /属性>
    < /&MBean的GT;
<项目>

预期成果是:book.01.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<项目>
    <的MBean code =org.jboss.naming.JNDIBindingServiceMgr
        NAME =abc.jndi:名称= JNDIProp>
        <属性名=mypropserialDataType =jbxb>
            < JNDI:绑定
                的xmlns:XS =htt​​p://www.w3.org/2001/XMLSchema-instance
                的xmlns:JNDI =瓮:JBoss的:JNDI绑定服务:1.0
                XS:的schemaLocation =瓮:JBoss的:JNDI绑定服务:1.0资源:JNDI的绑定service_1_0.xsd>
                < JNDI:结合
                    NAME =标题>
                    < JNDI:值类型=java.lang.String中>
                        ABC
                    < / JNDI:值>
                < / JNDI:结合>
                < JNDI:绑定名称=作者>
                    < JNDI:值类型=java.lang.String中>
                        约翰
                    < / JNDI:值>
                < / JNDI:结合>
            < / JNDI:绑定>
        < /属性>
    < /&MBean的GT;
<项目>

但想知道我应该如何使用相同的模板,当我想为book.02或ebook.03配置文件/目标运行的值。

请注意:Maven的配置文件名称/ Ant目标名称将匹配Template.xml中 @varaible @ 名称的变量。
例如: MVN -P book.01 蚂蚁ebook.01

更新:

使用@Ken XSL样式表文件的帮助下,我使用了XML的Maven插件改造template.xml.Using Maven的替代品 - 插件我是能够改变 @ book.01 @ 在Template.xml中varaible和配置文件名称替换它。

例如:

 <型材>
        <&ID GT; book.02< / ID>
        <活化GT;
            < activeByDefault>真< / activeByDefault>
        < /活化GT;
        <建立>
    <&插件GT;
        <&插件GT;
            <的groupId方式> com.google code.maven-替代-插件< /的groupId>
            <&的artifactId GT;&替代LT; / artifactId的>
            <&执行GT;
                <执行与GT;
                    <阶段> prepare包装和LT; /阶段>
                    <目标>
                        <&目标GT;更换< /目标>
                    < /目标>
                < /执行>
            < /处决>
            <结构>
                < ignoreMissingFile>真< / ignoreMissingFile>
                <文件>&Template.xml中LT; /文件>
                <&OUTPUTFILE GT;
                    目标/ Template.xml中
                < / OUTPUTFILE>
                <正则表达式>假LT; /正则表达式>
                <令牌GT; @ book.01 @< /令牌GT;
                < VALUE> @ book.02 @< /值>
            < /结构>
        < /插件>
    < /插件>


解决方案

您寻求帮助,或者你要求的人来解决整个问题的吗?很明显,你没有上述数据文件的工作,因为他们都不是良好的。你问的志愿者给你一个完整的答案之前解决您的XML。我建议在你尝试首先要解决的问题,然后提出问题的未来。你可以对被定向这样的其中的你在你的理解的问题,那你发布的文件至少会得到很好的形成。

一个解决方案是下面,我怀疑其他读者可以使用有兴趣的办法< XSL:分析串>

T:\\ ftemp>键入properties.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
<变量>
    <变量ID =标题>
      < book.01> ABC< /book.01>
      < book.02>&高清LT; /book.02>
      < ebook.03>&GHI LT; /ebook.03>
      < ebook.04> klmn< /ebook.04>
    < /变量>
    <变量ID =作者>
      < book.01>约翰和LT; /book.01>
      < book.02> JACK< /book.02>
      < ebook.03>西米< /ebook.03>
      < ebook.04>&劳拉LT; /ebook.04>
    < /变量>
< /变量>

T:\\ ftemp>键入Template.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
<项目>
    <的MBean code =org.jboss.naming.JNDIBindingServiceMgr
        NAME =abc.jndi:名称= JNDIProp>
        <属性名=mypropserialDataType =jbxb>
            < JNDI:绑定
                的xmlns:XS =htt​​p://www.w3.org/2001/XMLSchema-instance
                的xmlns:JNDI =瓮:JBoss的:JNDI绑定服务:1.0
                XS:的schemaLocation =瓮:JBoss的:JNDI绑定服务:1.0资源:JNDI的绑定service_1_0.xsd>
                < JNDI:结合
                    NAME =标题>
                    < JNDI:值类型=java.lang.String中>
                        @ book.01 @
                    < / JNDI:值>
                < / JNDI:结合>
                < JNDI:绑定名称=作者>
                    < JNDI:值类型=java.lang.String中>
                        @ book.01 @
                    < / JNDI:值>
                < / JNDI:结合>
            < / JNDI:绑定>
        < /属性>
    < /&MBean的GT;
< /项目>

T:\\ ftemp>调用xslt2 Template.xml中properties.xsl

 <?XML版本=1.0编码=UTF-8><项目>
    <的MBean code =org.jboss.naming.JNDIBindingServiceMgrNAME =abc.jndi:名称= JNDIProp>
        <属性名=mypropserialDataType =jbxb>
            < JNDI:绑定的xmlns:XS =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:JNDI =瓮:JBoss的:JNDI绑定服务:1.0XS:的schemaLocation =瓮: JBoss的:JNDI绑定服务:1.0资源:JNDI的绑定service_1_0.xsd>
                < JNDI:绑定名称=标题>
                    < JNDI:值类型=java.lang.String中>
                        ABC
                    < / JNDI:值>
                < / JNDI:结合>
                < JNDI:绑定名称=作者>
                    < JNDI:值类型=java.lang.String中>
                        约翰
                    < / JNDI:值>
                < / JNDI:结合>
            < / JNDI:绑定>
        < /属性>
    < /&MBean的GT;
< /项目>

T:\\ ftemp>键入properties.xsl

 <?XML版本=1.0编码=US-ASCII&GT?;
<的xsl:样式的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform
                版本=2.0>< XSL:关键NAME =道具匹配=变量/ *
         使用=CONCAT(../@ ID,'和; #xd;',姓名()。)/><的xsl:模板匹配=j:价值的xmlns:J =瓮:JBoss的:JNDI绑定服务:1.0>
  < XSL:复制>
    < XSL:复制的选择=@ */>
    < XSL:变量名=ID选择=../@名/​​>
    < XSL:分析串选择=。正则表达式=@ @(。*?)>
      < XSL:匹配子串>
        < XSL:价值 -
          选择=键('道具',CONCAT($ ID,'和; #xd;',正则表达式组(1)),
                      DOC('properties.xml中'))/>
      < / XSL:匹配子串>
      < XSL:不匹配的子串>
        <的xsl:value-of的选择= />中。
      < / XSL:不匹配的子串>
    < / XSL:分析弦>
  < / XSL:复制>
< / XSL:模板>< XSL:模板匹配=@ * |节点()>< - 身份的所有其他节点 - >
  < XSL:复制>
    < XSL:申请模板选择=@ * |节点()/>
  < / XSL:复制>
< / XSL:模板>< / XSL:样式>

T:\\ ftemp> REM完成!

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

<?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>type 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>call 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>type 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>rem Done!

这篇关于如何使用配置文件或目标,以获得XML属性的值文件中使用模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆