2011-09-08 48 views
1

我想通过使用XSLT在system-properties.xml中添加属性。xslt将文本添加到jboss属性中-service.xml

当前XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE server> 
<server> 
    <mbean code="org.jboss.varia.property.PropertyEditorManagerService" 
      name="jboss:type=Service,name=PropertyEditorManager"> 
    </mbean> 
     <mbean code="org.jboss.varia.property.SystemPropertiesService" 
     name="jboss:type=Service,name=SystemProperties"> 
    <attribute name="Properties"> 
     my.project.property=This is the value of my property 
    </attribute> 
    </mbean> 
</server> 

我要添加属性名称= “属性” 里面一个新的属性。

结果:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE server> 
<server> 
    <mbean code="org.jboss.varia.property.PropertyEditorManagerService" 
     name="jboss:type=Service,name=PropertyEditorManager"> 
    </mbean> 
    <mbean code="org.jboss.varia.property.SystemPropertiesService" 
     name="jboss:type=Service,name=SystemProperties"> 
    <attribute name="Properties"> 
     my.project.property=This is the value of my property 
     my.project.anotherProperty=This is the value of my other property 
    </attribute> 
    </mbean> 
</server> 

感谢。

+0

好问题,+1。查看我的答案,获取完整,简短的解决方案。 –

回答

0

这个简单的转换 - 身份规则硬道理:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pNewProp" select= 
"'my.project.anotherProperty=This is the value of my other property '"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="attribute[@name='Properties']"> 
     <attribute name="Properties"> 
      <xsl:apply-templates/> 
      <xsl:value-of select="$pNewProp"/> 
      <xsl:text>&#xA;</xsl:text> 
    </attribute> 
</xsl:template> 
</xsl:stylesheet> 

时所提供的XML文档应用:

<server> 
    <mbean code="org.jboss.varia.property.PropertyEditorManagerService"    name="jboss:type=Service,name=PropertyEditorManager"></mbean> 
    <mbean code="org.jboss.varia.property.SystemPropertiesService"   name="jboss:type=Service,name=SystemProperties"> 
     <attribute name="Properties"> 
     my.project.property=This is the value of my property 
    </attribute> 
    </mbean> 
</server> 

产生想要的,正确的结果

<server> 
    <mbean code="org.jboss.varia.property.PropertyEditorManagerService" name="jboss:type=Service,name=PropertyEditorManager"/> 
    <mbean code="org.jboss.varia.property.SystemPropertiesService" name="jboss:type=Service,name=SystemProperties"> 
     <attribute name="Properties"> 
     my.project.property=This is the value of my property 
    my.project.anotherProperty=This is the value of my other property 
</attribute> 
    </mbean> 
</server>