使用xsl-and-string和regex将变量替换为属性文件中的值



目前我正在处理一个项目,该项目需要用属性文件中的值替换变量,为此,我认为xsl-analysis字符串是使用正则表达式替换变量的好选择。

这是我的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>

这是我当前的属性.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中所做的<xsl:template match="attribute">是否有效?。当我运行这个文件时,我得到以下输出:

<?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@" 的变量

我正在尝试获取从properties.xml文件到output.xml的所有变量的<mybook.01>的值。

预期的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>
样式表
<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>

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

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

相关内容

  • 没有找到相关文章

最新更新