如何根据输入xml中的条件添加新元素
我需要根据输入xml元素值条件插入新元素。请帮助我 我需要检查policy/transactionSplitTrans/sourceSystemCd/code='SCBP',如果为true,我需要在policy\tag下添加policyOperationalDatabasecd/code下的元素 输入xml:如何根据输入xml中的条件添加新元素,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我需要根据输入xml元素值条件插入新元素。请帮助我 我需要检查policy/transactionSplitTrans/sourceSystemCd/code='SCBP',如果为true,我需要在policy\tag下添加policyOperationalDatabasecd/code下的元素 输入xml: <?xml version="1.0" encoding="utf-8"?> <policies> <policy> <p
<?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>
非常感谢拉姆森。我的要求中还有一个条件。如果上述条件不成立,我需要检查policy/policyKey/policyFormCd/code='UB'我需要在policy标签下输出相同的底层policyOperationalDatabaseCD/code='SCBP'。我已经编辑了我的答案。今后,请在您的问题中包括所有条件。非常感谢Lamsen。对不起,失礼。您好,欢迎来到StackOverflow!如果您认为此答案有用,请勾选左侧箭头下方的复选标记(使其显示为绿色)。这样,其他用户就可以知道这是公认的答案。