如果使用xslt已经存在元素的值,如何保持元素的值

时间:2014-06-25 13:08:28

标签: xml xslt

我希望保留UserType标记的值,如果其当前设置为xslt中提到的默认值。它不起作用。

Xml:

<FlatRateSMSConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="..\..\..\schema\FlatRateSMSConfig.xsd">
    <Configuration id="default">
        <SMSServiceDays>30</SMSServiceDays>
        <SMSServiceCharge>100</SMSServiceCharge>
        <MinimumBalance>200</MinimumBalance>    
        <ServiceClassMap>
            <ServiceClass VoiceService="100" FlatRateService="200"/>    
        </ServiceClassMap>  
        <NoInputTimeout>3</NoInputTimeout>
        <MaxTimeoutRetries>3</MaxTimeoutRetries>    
        <MaxInvalidRetries>3</MaxInvalidRetries>
        <MainMenuDTMF>*</MainMenuDTMF>
        <ConfirmDTMF>0</ConfirmDTMF>
        <NegateDTMF>1</NegateDTMF>
        <MnpDaId>122</MnpDaId>
        <LtdDaId>233</LtdDaId>
        <FlatRateDataServiceConfiguration>
            <ServiceSpecificData>
                <ServiceClassData  dtmf="2" minimumBalance="80" promptId="USER_FRDS_MORE_DAYS_MSG" refillProfileId="E2" serviceClass="10" serviceDays="7" transactionAmount="26" transactionCurrency="EU2R" />
            </ServiceSpecificData>
            <UserType>SmartPhone Users 232</UserType>
        </FlatRateDataServiceConfiguration>
    </Configuration>
</FlatRateSMSConfiguration>

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xalan="http://xml.apache.org/xalan">
    <xsl:output method="xml" indent="yes" xalan:indent-amount="2" omit-xml-declaration="no"/>
    <xsl:template match="FlatRateSMSConfiguration">
        <FlatRateSMSConfiguration>
            <xsl:attribute name="xsi:noNamespaceSchemaLocation"><xsl:value-of select="@xsi:noNamespaceSchemaLocation"/></xsl:attribute>
            <xsl:for-each select="Configuration">
                <xsl:element name="Configuration">
                    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>   
                    <xsl:copy-of select="SMSServiceDays"/>
                    <xsl:copy-of select="SMSServiceCharge"/>
                    <xsl:copy-of select="MinimumBalance"/>
                    <xsl:copy-of select="ServiceClassMap"/>
                    <xsl:copy-of select="NoInputTimeout"/>
                    <xsl:copy-of select="MaxTimeoutRetries"/>
                    <xsl:copy-of select="MaxInvalidRetries"/>
                    <xsl:copy-of select="MainMenuDTMF"/>
                    <xsl:copy-of select="ConfirmDTMF"/>
                    <xsl:copy-of select="NegateDTMF"/>
                    <xsl:choose>
                        <xsl:when test="MnpDaId">
                            <xsl:copy-of select="MnpDaId"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <MnpDaId>1</MnpDaId>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when test="LtdDaId">
                            <xsl:copy-of select="LtdDaId"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <LtdDaId>2</LtdDaId>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when test="FlatRateDataServiceConfiguration">
                            <xsl:element name="FlatRateDataServiceConfiguration">
                                <xsl:element name="ServiceSpecificData">
                                    <xsl:for-each select="FlatRateDataServiceConfiguration/ServiceSpecificData/ServiceClassData">
                                        <xsl:element name="ServiceClassData">
                                            <xsl:attribute name="dtmf"><xsl:value-of select="@dtmf"/></xsl:attribute>
                                            <xsl:attribute name="minimumBalance"><xsl:value-of select="@minimumBalance"/></xsl:attribute>
                                            <xsl:attribute name="promptId"><xsl:value-of select="@promptId"/></xsl:attribute>
                                            <xsl:attribute name="refillProfileId"><xsl:value-of select="@refillProfileId"/></xsl:attribute>
                                            <xsl:attribute name="serviceClass"><xsl:value-of select="@serviceClass"/></xsl:attribute>
                                            <xsl:attribute name="serviceDays"><xsl:value-of select="@serviceDays"/></xsl:attribute>
                                            <xsl:attribute name="transactionAmount"><xsl:value-of select="@transactionAmount"/></xsl:attribute>
                                            <xsl:attribute name="transactionCurrency"><xsl:value-of select="@transactionCurrency"/></xsl:attribute>
                                            <xsl:choose>
                                                <xsl:when test="@planType">
                                                    <xsl:attribute name="planType"><xsl:value-of select="@planType"/></xsl:attribute>
                                                    <xsl:attribute name="pamServiceId"><xsl:value-of select="@pamServiceId"/></xsl:attribute>
                                                    <xsl:attribute name="offerId"><xsl:value-of select="@offerId"/></xsl:attribute>
                                                    <xsl:attribute name="terminationPromptId"><xsl:value-of select="@terminationPromptId"/></xsl:attribute>
                                                </xsl:when>
                                                <xsl:otherwise>
                                                    <xsl:attribute name="planType">NORMAL</xsl:attribute>
                                                    <xsl:attribute name="pamServiceId">1</xsl:attribute>
                                                    <xsl:attribute name="offerId">1</xsl:attribute>
                                                    <xsl:attribute name="terminationPromptId">USER_FRDS_FINAL_MSG</xsl:attribute>
                                                </xsl:otherwise>
                                            </xsl:choose>
                                        </xsl:element>
                                    </xsl:for-each>
                                </xsl:element>
                                <xsl:choose>
                                    <xsl:when test="./FlatRateDataServiceConfiguration">
                                        <xsl:copy-of select="UserType"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <UserType>SmartPhone Users</UserType>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:element>
                        </xsl:when>
                        <xsl:otherwise>
                            <FlatRateDataServiceConfiguration>
                                <ServiceSpecificData>
                                    <ServiceClassData planType="NORMAL" dtmf="2" minimumBalance="80" promptId="USER_FRDS_SEVEN_DAYS_MSG" refillProfileId="E1" serviceClass="10" serviceDays="7" transactionAmount="26" transactionCurrency="EUR" pamServiceId="1" offerId="1" terminationPromptId="FRDS_MORE_DAYS"/>                                    
                                </ServiceSpecificData>
                                <UserType>2G</UserType>
                            </FlatRateDataServiceConfiguration>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:element>          
            </xsl:for-each>         
        </FlatRateSMSConfiguration> 
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

xsl:for-each中设置的上下文为Configuration。您必须考虑选择XPath的值FlatRateDataServiceConfiguration。在您当前的XSLT中,您在for-each

中包含此块
<xsl:for-each select="Configuration">
    ...
    <xsl:choose>
        <xsl:when test="./FlatRateDataServiceConfiguration">
            <xsl:copy-of select="UserType"/> <!-- UserType will not be copied -->
        </xsl:when>
        <xsl:otherwise>
            <UserType>SmartPhone Users</UserType>
        </xsl:otherwise>
    </xsl:choose>
    ...

UserType的绝对路径是

/FlatRateSMSConfiguration/Configuration/FlatRateDataServiceConfiguration/UserType

由于您位于Configuration内(由包含for-each块的xsl:choose建立的上下文),您可以使用UserType的相对路径,该路径应为{ {1}}:

FlatRateDataServiceConfiguration/UserType

在您的代码中,您试图获得:

<xsl:for-each select="Configuration">
    ...
    <xsl:choose>
        <xsl:when test="./FlatRateDataServiceConfiguration">
            <xsl:copy-of select="FlatRateDataServiceConfiguration/UserType"/>
        </xsl:when>
        <xsl:otherwise>
            <UserType>SmartPhone Users</UserType>
        </xsl:otherwise>
    </xsl:choose>
...

并不存在。