提问人:Prashant Tripathi 提问时间:11/4/2023 最后编辑:Prashant Tripathi 更新时间:11/4/2023 访问量:58
在父元素下复制元素时删除 xmlns 属性。并根据条件执行一些 xslt 语句
Removal of xmlns attribute while copying an element under a parent element. And execution of some xslt statements based on condition
问:
我是 XSLT 的新手,因此需要有关此问题的帮助
我有以下输入XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SubmitImportJobRequest xmlns="/cp/bdtbeans">
<User>abc</User>
<Client>qwe</Client>
<Password>00000</Password>
<JobName>CMBX_TEST</JobName>
<UserAssets Owner="abc" EffectiveStartDate="2023-04-03">
<CDSwaps>
<CDS ID="28881002_8" IDType="LOCALID" Name="Buy Protection on Apple Inc" BARRAID="28881002_8" StartDate="2023-12-22" ExpirationDate="2028-12-22" ContractSize="1" AccrualBasis="ACT/360" PriceCurrency="USD" UnderlierID="US037833EJ59" UnderlierIDType="ISIN" DealSpread="100"/>
</CDSwaps>
<CMBXs>
<CMBX ID="32083535_8" IDType="LOCALID" Name="Sell Protection on CMBX.NA.AAA Indices Series 15 V1" BARRAID="32083535_8" ContractSize="1" RecoveryRate="40" PriceCurrency="USD" StartDate="2022-12-20" ExpirationDate="2028-03-09" UnderlierID="137BENAO4" UnderlierIDType="MIP" DealSpread="50"/>
</CMBXs>
</UserAssets>
</SubmitImportJobRequest>
</s:Body>
</s:Envelope>
当前输出 XML
<Logical>
<UserInstruments Effective_Start_Date="2023-04-03" Owner="paqa">
<CDSwaps xmlns="/cp/bdtbeans">
<CDS AccrualBasis="ACT/360" Priority="BARRA" BARRAID="28881002_8" ContractSize="1" CouponFrequency="3M" DealSpread="100" ExpirationDate="2028-12-22" ID="28881002_8" IDType="LOCALID" Name="Buy Protection on Apple Inc" PriceCurrency="USD" RecoveryRate="4.0E1" StartDate="2023-12-22" Und_ID="US037833EJ59" Und_IDType="ISIN"/>
<CDS xmlns="" Priority="BARRA" BARRAID="32083535_8" ContractSize="1" DealSpread="50" ExpirationDate="2028-03-09" ID="32083535_8" IDType="LOCALID" IsCMBX="true" Name="Sell Protection on CMBX.NA.AAA Indices Series 15 V1" PriceCurrency="USD" RecoveryRate="40" StartDate="2022-12-20" Und_ID="137BENAO4" Und_IDType="MIP"/>
</CDSwaps>
</UserInstruments>
</Logical>
期望输出
<Logical>
<UserInstruments Effective_Start_Date="2023-04-03" Owner="paqa">
<CDSwaps xmlns="/cp/bdtbeans">
<CDS AccrualBasis="ACT/360" Priority="BARRA" BARRAID="28881002_8" ContractSize="1" CouponFrequency="3M" DealSpread="100" ExpirationDate="2028-12-22" ID="28881002_8" IDType="LOCALID" Name="Buy Protection on Apple Inc" PriceCurrency="USD" RecoveryRate="4.0E1" StartDate="2023-12-22" Und_ID="US037833EJ59" Und_IDType="ISIN"/>
<CDS Priority="BARRA" BARRAID="32083535_8" ContractSize="1" DealSpread="50" ExpirationDate="2028-03-09" ID="32083535_8" IDType="LOCALID" IsCMBX="true" Name="Sell Protection on CMBX.NA.AAA Indices Series 15 V1" PriceCurrency="USD" RecoveryRate="40" StartDate="2022-12-20" Und_ID="137BENAO4" Und_IDType="MIP"/>
</CDSwaps>
</UserInstruments>
</Logical>
用于此转换的 XSLT
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bdt="/cp/bdtbeans">
<xsl:template match="node() | @*" >
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bdt:CDSwaps">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
<xsl:apply-templates select="//bdt:CMBX"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bdt:CMBXs"/>
<xsl:template match="bdt:CMBX">
<xsl:element name="CDS">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
在 CDSwaps 下移动 CMBX 元素时,添加了一个额外的属性 xmlns=“”,我不想要这个属性。 如何在 XSLT 中应用条件,以便仅当输入 XML 中存在 CDSwaps 和 CMBX 标记时,才应发生所有这些复制和重命名 CMBX 元素的语句?
任何帮助将不胜感激。
答:
0赞
michael.hor257k
11/4/2023
#1
首先,当前输出根本不是你所说的那样。
接下来,当你这么说时:
在 CDSwaps 下移动 CMBX 元素时,添加了一个额外的属性 xmlns=“”,我不想要这个属性。
你完全误诊了问题。这里真正发生的事情是,元素及其所有后代都在命名空间中,并且在复制时它们仍保留在该命名空间中。SubmitImportJobRequest
/cp/bdtbeans
但是,当您创建一个新元素来用作从该元素移动的节点的包装器时,您将在 no-namespace 中创建一个元素 - 处理器必须将其标记为这样。CDS
CMBX
要在与其祖先(和潜在的后代)元素相同的命名空间中创建包装器,请更改以下内容:
<xsl:element name="CDS">
自:
<xsl:element name="CDS" namespace="/cp/bdtbeans">
评论
0赞
Prashant Tripathi
11/6/2023
谢谢你的解释。你能帮我解决这个问题的第二部分,即我如何在XSLT中应用条件,以便所有这些复制和重命名CMBX元素的语句都应该只在输入XML中存在CDSwaps和CMBXs标签时发生吗?
0赞
michael.hor257k
11/6/2023
不。请就不相关的问题单独提出问题。
0赞
Prashant Tripathi
11/8/2023
嗨 @michael.hor257k 这是我 stackoverflow.com/questions/77440221/ 的单独问题......
0赞
Prashant Tripathi
11/9/2023
请帮助我解决这个问题,stackoverflow.com/questions/77440221/...
评论