如何根据 XSLT 中的字段值跳过段

How to Skip segment based on a field value in XSLT

我有一个 XML 输入,我必须将其映射到目标结构中

这是给 Fiddler 的link:LINK

<?xml version='1.0' encoding='UTF-8'?>
<root>
    <d>
        <Campaign>
            <CampaignId>0000000112</CampaignId>
            <Name>Connected Water DMI Fail Test</Name>
            <MarketingAreaId>CXXGLOBAL</MarketingAreaId>
            <SegmentationObject>SAP_CONTACT_ENGAGEMENT_SIN</SegmentationObject>
            <ImplementationId>ZOC_EXPORT_ACOUSTIC</ImplementationId>
        </Campaign>
        <PackageId>47</PackageId>
        <ExecutionStartDateTime>2021-06-30T15:28:49Z</ExecutionStartDateTime>
        <ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
        <CampaignTargetGroupMembers>
            <OutboundId>A071C1B1F437B581DC597E5116B38BED039CA864</OutboundId>
            <PackageId>47</PackageId>
            <ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
            <TargetGroupMemberAttributeData>
                <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR</AttributeId>
                <Value></Value>
                <EdmTypeId>Edm.String</EdmTypeId>
                <OutboundId>A071C1B1F437B581DC597E5116B38BED039CA864</OutboundId>
            </TargetGroupMemberAttributeData>
            <TargetGroupMemberAttributeData>
                <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_LAST</AttributeId>
                <Value>Bailey</Value>
                <EdmTypeId>Edm.String</EdmTypeId>
                <OutboundId>A071C1B1F437B581DC597E5116B38BED039CA864</OutboundId>
            </TargetGroupMemberAttributeData>
            <TargetGroupMemberAttributeData>
                <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_FIRST</AttributeId>
                <Value>Chandler</Value>
                <EdmTypeId>Edm.String</EdmTypeId>
                <OutboundId>A071C1B1F437B581DC597E5116B38BED039CA864</OutboundId>
            </TargetGroupMemberAttributeData>
        </CampaignTargetGroupMembers>
        <CampaignTargetGroupMembers>
            <OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
            <PackageId>47</PackageId>
            <ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
            <TargetGroupMemberAttributeData>
                <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR</AttributeId>
                <Value>rcgrymm@gmail.com</Value>
                <EdmTypeId>Edm.String</EdmTypeId>
                <OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
            </TargetGroupMemberAttributeData>
            <TargetGroupMemberAttributeData>
                <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_LAST</AttributeId>
                <Value>Carmona</Value>
                <EdmTypeId>Edm.String</EdmTypeId>
                <OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
            </TargetGroupMemberAttributeData>
            <TargetGroupMemberAttributeData>
                <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_FIRST</AttributeId>
                <Value>Ruben</Value>
                <EdmTypeId>Edm.String</EdmTypeId>
                <OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
            </TargetGroupMemberAttributeData>                           
        </CampaignTargetGroupMembers>
    </d>
</root>

我必须编写 XSLT 代码,以便节点和子节点仅在 AttributeId = DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR 和相应的值 != ''

时被复制并使用新的字段名称进行格式化

如何修改 XSLT 代码:

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

      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
        <xsl:variable name="campaign" select="root/d/Campaign/CampaignId/text()" />
        <xsl:template match="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_FIRST']">
            <FIRST_NAME><xsl:value-of select="Value"/></FIRST_NAME>
       </xsl:template>  
       <xsl:template match="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_LAST']">
            <LAST_NAME><xsl:value-of select="Value" /></LAST_NAME>
     <xsl:template match="CampaignTargetGroupMembers/ExecutionRunKey">
            <CAMPAIGN_ID><xsl:copy-of select="$campaign"/></CAMPAIGN_ID>
       </xsl:template> 
       <xsl:template match="TargetGroupMemberAttributeData[AttributeId='OUTBOUND_INTERACTION']">
            <UUID><xsl:value-of select="Value" /></UUID>
       </xsl:template> 
    </xsl:stylesheet>

The output Should be:

<?xml version="1.0" encoding="UTF-8"?><root>
    <d>
        <Campaign>
            <CampaignId>0000000112</CampaignId>
            <Name>Connected Water DMI Fail Test</Name>
            <MarketingAreaId>CXXGLOBAL</MarketingAreaId>
            <SegmentationObject>SAP_CONTACT_ENGAGEMENT_SIN</SegmentationObject>
            <ImplementationId>ZOC_EXPORT_ACOUSTIC</ImplementationId>
        </Campaign>
        <PackageId>47</PackageId>
        <ExecutionStartDateTime>2021-06-30T15:28:49Z</ExecutionStartDateTime>
        <ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
        <CampaignTargetGroupMembers>
            <OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
            <PackageId>47</PackageId>
            <CAMPAIGN_ID>0000000112</CAMPAIGN_ID>
            <Email>rcgrymm@gmail.com</Email>
            <LAST_NAME>Carmona</LAST_NAME>
            <FIRST_NAME>Ruben</FIRST_NAME>
            <TargetGroupMemberAttributeData>
                <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-INITIATIVE_ID</AttributeId>
                <Value>0000000105,0000000092,0000000041,,0000000054,0000000076</Value>
                <EdmTypeId>Edm.String</EdmTypeId>
                <OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
            </TargetGroupMemberAttributeData>           
        </CampaignTargetGroupMembers>
    </d>
</root>

要获得所需的输出,您可以使用以下样式表。但并非所有值都可以从您的源中导出,因此我输入了一些静态值。我想你可以填写其余部分。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="campaign" select="root/d/Campaign/CampaignId/text()" />
    <xsl:mode on-no-match="shallow-copy" />
    <xsl:strip-space elements="*" />

    <xsl:template match="CampaignTargetGroupMembers | TargetGroupMemberAttributeData" />

    <xsl:template match="CampaignTargetGroupMembers[TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR' and Value!='']]">
      <xsl:copy>
          <xsl:apply-templates select="@* | OutboundId | PackageId"/>
          <CAMPAIGN_ID><xsl:value-of select="$campaign" /></CAMPAIGN_ID>
          <Email><xsl:value-of select="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR']/Value" /></Email>
          <LAST_NAME><xsl:value-of select="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_LAST']/Value" /></LAST_NAME>
          <FIRST_NAME><xsl:value-of select="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_FIRST']/Value"/></FIRST_NAME>
          <TargetGroupMemberAttributeData>
            <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-INITIATIVE_ID</AttributeId>
            <Value>0000000105,0000000092,0000000041,,0000000054,0000000076</Value>
            <EdmTypeId><xsl:value-of select="TargetGroupMemberAttributeData[1]/EdmTypeId" /></EdmTypeId>
            <OutboundId><xsl:value-of select="TargetGroupMemberAttributeData[1]/OutboundId" /></OutboundId>
          </TargetGroupMemberAttributeData> 
      </xsl:copy>
    </xsl:template>    
       
</xsl:stylesheet>

它的输出是:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <d>
        <Campaign>
            <CampaignId>0000000112</CampaignId>
            <Name>Connected Water DMI Fail Test</Name>
            <MarketingAreaId>CXXGLOBAL</MarketingAreaId>
            <SegmentationObject>SAP_CONTACT_ENGAGEMENT_SIN</SegmentationObject>
            <ImplementationId>ZOC_EXPORT_ACOUSTIC</ImplementationId>
        </Campaign>
        <PackageId>47</PackageId>
        <ExecutionStartDateTime>2021-06-30T15:28:49Z</ExecutionStartDateTime>
        <ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
        <CampaignTargetGroupMembers>
            <OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
            <PackageId>47</PackageId>
            <CAMPAIGN_ID>0000000112</CAMPAIGN_ID>
            <Email>rcgrymm@gmail.com</Email>
            <LAST_NAME>Carmona</LAST_NAME>
            <FIRST_NAME>Ruben</FIRST_NAME>
            <TargetGroupMemberAttributeData>
                <AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-INITIATIVE_ID</AttributeId>
                <Value>0000000105,0000000092,0000000041,,0000000054,0000000076</Value>
                <EdmTypeId>Edm.String</EdmTypeId>
                <OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
            </TargetGroupMemberAttributeData>
        </CampaignTargetGroupMembers>
    </d>
</root>

这是我们想要的(有些静态值无法从输入 XML 中推导出来)。