如何根据输入xml中的条件添加新元素
问题描述:
我需要根据输入的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>
答
所有你需要的是一个身份模板,模板匹配目标节点以及目标节点下的条件,如果条件为真,则将插入附加节点。如:
<?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>
非常感谢Lamsen。如果上述条件不成立,我需要检查策略/ policyKey/policyFormCd/code ='UB',我需要在策略标记下输出相同的underlyingPolicyOperationalDatabaseCd/code ='SCBP'。 – 2014-12-08 07:51:41
我编辑了我的答案。今后,请在您的问题中包含所有条件。 – 2014-12-08 08:11:22
非常感谢Lamsen.Sorry的不便之处。 – 2014-12-08 08:41:43