目前我正在开发一个需要使用属性文件中的值替换变量的项目,对于这种用法,我认为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,'
',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,'
',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@"
我试图将所有变量的<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>
答案 0 :(得分:2)
样式表
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:key name="props" match="variable/*"
use="concat(../@id,'
',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),'
',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,'
',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),'
',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>