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>

如果要保留注释,则需要更改模板以处理元素节点的textattribute节点:

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