xsl用props.xml中的值替换变量

时间:2013-09-17 15:18:07

标签: xml xslt xslt-1.0 xslt-2.0

我正在尝试使用xsl使用props.xml中的值重新声明@env@

这是我当前的source.xml

<projects>
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="amfam.cbsconfig.jndi:name=cbsconfigJNDI">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
                           xmlns:jndi="urn:jboss:jndi-binding-service:1.0" 
                           xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">
                <jndi:binding name="books/category/config">
                    <java:properties xmlns:java="urn:jboss:java-properties" 
                                 xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
                                 xs:schemaLocation="urn:jboss:java-properties resource:java-properties_1_0.xsd">
                        <java:property>
                            <java:key>url</java:key>
                            <java:value>@env@</java:value>
                        </java:property>
                        <java:property>
                            <java:key>url.port</java:key>
                            <java:value>@env@</java:value>
                        </java:property>
                        <java:property>
                            <java:key>category</java:key>
                            <java:value>@env@</java:value>
                        </java:property>
                        <java:property>
                            <java:key>sub-category</java:key>
                            <java:value>@env@</java:value>
                        </java:property>
                        <java:property>
                            <java:key>county</java:key>
                            <java:value>@env@</java:value>
                        </java:property>
                        <java:property>
                            <java:key>zip</java:key>
                            <java:value>@env@</java:value>
                        </java:property>
                    </java:properties>               
                </jndi:binding>
            </jndi:bindings>
        </attribute>
    </mbean>
</projects>

我尝试使用以下xsl代码: 但不知何故,他们没有从props.xml中提取任何值。

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:j="urn:jboss:jndi-binding-service:1.0"
                version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>

    <xsl:key name="props" match="variable/*"
             use="concat(../@id,'&#xd;',name(.))"/>          
    <xsl:template match="java:value" xmlns:java="urn:jboss:java-properties">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:variable name="id" select="../java:key"/>
            <xsl:analyze-string select="." regex="@(.*?)@">
                <xsl:matching-substring>
                    <xsl:value-of
              select="key('props',concat($id,'&#xd;',regex-group(1)),
                          doc('props.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()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

以下是来自:props.xml

的值
<?xml version="1.0" encoding="UTF-8"?>
<variables>
    <variable id="url">
        <env>abc.com</env>
    </variable>
    <variable id="url.port">
        <env>1234</env>
    </variable>
    <variable id="category">
        <env>books</env>
    </variable>
    <variable id="sub-category">
        <env>ebooks</env>
    </variable>
    <variable id="county">
        <env>zipling</env>
    </variable>
    <variable id="zip">
        <env>00000</env>
    </variable>
</variables>

1 个答案:

答案 0 :(得分:1)

是不是因为这条线......

<xsl:variable name="id" select="../@name"/>

您位于 java:value 元素上,但父元素没有名称属性。看起来你需要在这里使用 java:key 元素,因为这与你的props.xml中的 id 相对应。

<xsl:variable name="id" select="../java:key"/>
相关问题