如何根据输入xml中的条件添加新元素

时间:2014-12-08 07:29:22

标签: xml xslt xslt-1.0

我需要根据输入的xml元素值条件插入新元素。请帮助我。

我需要检查policy / transactionSplitTrans / sourceSystemCd / code ='SCBP',如果为true,则需要在policy \ tag下添加元素underlyingPolicyOperationalDatabaseCd / code。

输入xml:

<?xml version="1.0" encoding="utf-8"?>
<policies>
    <policy>
        <policyKey>
            <policyNbr>004567</policyNbr>
            <policyEffectiveDt>2014-11-14</policyEffectiveDt>
            <policyID>54545</policyID>
            <policyFormCd>
                <code>669</code>
            </policyFormCd>
        </policyKey>
        <transactionSplitTrans>
            <sourceSystemCd>
                <code>SCBP</code>
            </sourceSystemCd>
        </transactionSplitTrans>
    </policy> 
</policies>

预期的O / p:

<policies>
    <policy>
        <policyKey>
            <policyNbr>004567</policyNbr>
            <policyEffectiveDt>2014-11-14</policyEffectiveDt>
            <policyID>54545</policyID>
            <policyFormCd>
                <code>669</code>
            </policyFormCd>
        </policyKey>
        <transactionSplitTrans>
            <sourceSystemCd>
                <code>SCBP</code>
            </sourceSystemCd>
        </transactionSplitTrans>
        <underlyingPolicyOperationalDatabaseCd>
            <code>SCBP</code>
        </underlyingPolicyOperationalDatabaseCd>
    </policy> 
</policies>

尝试过xslt:

<xsl:stylesheet version="1.0" xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" 
    extension-element-prefixes="dp str xsd xsi ps dpconfig pol" 
    exclude-result-prefixes="dp dpconfig xsi soap xsd xsi str pol dpconfig xsl a b c d e f g h i j k l m n o p q r s t u v w x y z A B" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="underlyingPolicyOperationalDatabaseCd">underlyingPolicyOperationalDatabaseCd</xsl:param>
    <xsl:param name="code">code</xsl:param>

    <xsl:template match="@* | node()" name="Copy">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//policy">
        <xsl:call-template name="Copy"/>
        <xsl:choose>
            <xsl:when test="./transactionSplitTrans/sourceSystemCd[code='SCBP']">
                <xsl:element name="{$underlyingPolicyOperationalDatabaseCd}">
                    <xsl:element name="{$code}">
                    <xsl:value-of select="'SCBP'" />
                    </xsl:element>
                </xsl:element>
            </xsl:when>
        </xsl:choose>

    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

您只需要一个标识模板,一个匹配目标节点的模板,以及目标节点下的条件,如果条件为真,它将插入其他节点。如:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output omit-xml-declaration="yes"/>

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="transactionSplitTrans">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
        <xsl:if test="descendant::code='SCBP' or preceding-sibling::policyKey/policyFormCd/code='UB'">
            <underlyingPolicyOperationalDatabaseCd>
                <code>SCBP</code>
            </underlyingPolicyOperationalDatabaseCd>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>
相关问题