0

I am trying to repalce a varaible @env@ with the values from props.xml using xsl.

This is my current 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>

I have tried this using with the following xsl code: but somehow they are not pulling any values from 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>

and here are the values that are getting from: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>
4

1 回答 1

1

是不是因为这条线...

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

您位于java:value元素上,但父元素没有name属性。看起来它是您需要在此处使用的java:key元素,因为它与您的 props.xml 中的id相对应。

<xsl:variable name="id" select="../java:key"/>
于 2013-09-17T16:09:02.093 回答