2014-12-08 55 views
0

我需要根據輸入的xml元素值condition.please幫助我插入新元素。如何根據輸入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> 

回答

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> 
+0

非常感謝Lamsen。如果上述條件不成立,我需要檢查策略/ policyKey/policyFormCd/code ='UB',我需要在策略標記下輸出相同的underlyingPolicyOperationalDatabaseCd/code ='SCBP'。 – 2014-12-08 07:51:41

+0

我編輯了我的答案。今後,請在您的問題中包含所有條件。 – 2014-12-08 08:11:22

+0

非常感謝Lamsen.Sorry的不便之處。 – 2014-12-08 08:41:43

相關問題