0

現在、プロパティファイルの値で変数を置換する必要があるプロジェクトに取り組んでいます。この使用法では、xsl analyze-string が正規表現を使用した変数の置換に適していると考えました。

これは私のsource.xmlファイルです:

<?xml version="1.0" encoding="UTF-8"?>
<projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" 
 name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=@mybook.01@
        <!-- properties for def.com -->
        book1.int=@mybook.01@
        <!-- properties for ghi.com -->
        book1.qa=@mybook.01@
        <!-- properties for jkl.com -->
        book1.prod=@mybook.01@
    </attribute>
</mbean>
<projects>

これは私の properties.xml ファイルです:

<?xml version="1.0" encoding="UTF-8"?>
<variables>
    <variable id="book1.dev">
        <mybook.01>123</mybook.01>
        <mybook.02>456</mybook.02>
    </variable>
    <variable id="book1.int">
        <mybook.01>789</mybook.01>
        <mybook.02>346</mybook.02>
    </variable>
    <variable id="book1.qa">
        <mybook.01>ab2</mybook.01>
        <mybook.02>45ff</mybook.02>
    </variable>
    <variable id="book1.prod">
        <mybook.01>rt67</mybook.01>
        <mybook.02>hgj8</mybook.02>
    </variable> 
</variables>

これは私の現在の 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="attribute">
  <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>

この xsl ファイルで私がやろうとしているのは@(.*)@、source.xml で一致する文字列を見つけて、これを properties.xml ファイルの一致するプロパティ値に置き換えることです。

<xsl:template match="attribute">がxslでやっていることが有効かどうか教えてください。このファイルを実行すると、次の出力が得られます。

<?xml version="1.0" encoding="UTF-8"?>
<projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" 
 name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        book1.dev=

        book1.int=

        book1.qa=

        book1.prod=
    </attribute>
</mbean>
<projects>

@Martin からの入力に従って、追加情報を追加します。

ソース xml には、次の変数があります。"@mybook.01@"

<mybook.01>すべての変数の値を properties.xml ファイルから output.xmlに取得しようとしています。

予期される output.xml ファイルは次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" 
 name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=123
        <!-- properties for def.com -->
        book1.int=789
        <!-- properties for ghi.com -->
        book1.qa=ab2
        <!-- properties for jkl.com -->
        book1.prod=rt67
    </attribute>
</mbean>
<projects>
4

1 に答える 1

2

スタイルシート

<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="attribute">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:variable name="id" select="../@name"/>
    <xsl:analyze-string select="." regex="([\w.]+)=@(.*?)@">
      <xsl:matching-substring>
        <xsl:value-of
          select="concat(regex-group(1), '=', key('props',concat(regex-group(1),'&#xd;',regex-group(2)),
                      doc('test2013081402.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>

変形する

<projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" 
 name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=@mybook.01@
        <!-- properties for def.com -->
        book1.int=@mybook.01@
        <!-- properties for ghi.com -->
        book1.qa=@mybook.01@
        <!-- properties for jkl.com -->
        book1.prod=@mybook.01@
    </attribute>
</mbean>
</projects>

の中へ

<?xml version="1.0" encoding="UTF-8"?><projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">

        book1.dev=123

        book1.int=789

        book1.qa=ab2

        book1.prod=rt67
    </attribute>
</mbean>
</projects>

test2013081402.xmlプロパティドキュメントはどこにありますか

<variables>
    <variable id="book1.dev">
        <mybook.01>123</mybook.01>
        <mybook.02>456</mybook.02>
    </variable>
    <variable id="book1.int">
        <mybook.01>789</mybook.01>
        <mybook.02>346</mybook.02>
    </variable>
    <variable id="book1.qa">
        <mybook.01>ab2</mybook.01>
        <mybook.02>45ff</mybook.02>
    </variable>
    <variable id="book1.prod">
        <mybook.01>rt67</mybook.01>
        <mybook.02>hgj8</mybook.02>
    </variable> 
</variables>

コメントを保持したい場合は、要素ノードのtext子ノードを処理するようにテンプレートを変更する必要があります。attribute

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

<xsl:template match="attribute/text()">
    <xsl:analyze-string select="." regex="([\w.]+)=@(.*?)@">
      <xsl:matching-substring>
        <xsl:value-of
          select="concat(regex-group(1), '=', key('props',concat(regex-group(1),'&#xd;',regex-group(2)),
                      doc('test2013081402.xml')))"/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>


</xsl:stylesheet>

出力の上の最新のスタイルシート

<?xml version="1.0" encoding="UTF-8"?><projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=123
        <!-- properties for def.com -->
        book1.int=789
        <!-- properties for ghi.com -->
        book1.qa=ab2
        <!-- properties for jkl.com -->
        book1.prod=rt67
    </attribute>
</mbean>
</projects>
于 2013-08-14T17:28:24.707 に答える