添加 xsi:nil="true";对于空元素 [英] Add xsi:nil="true" for empty elements

查看:41
本文介绍了添加 xsi:nil="true";对于空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在想办法为空元素添加 xsi:nil="true" .我可以在输出上编写一个单独的 xsl 来实现这一点,但我试图弄清楚是否可以将它写在当前的 xsl 本身中.

I am trying to figure out a way to add xsi:nil="true" for empty elements. I could write a separate xsl on the output to achieve this, but trying to figure out if I could have it written in the current xsl itself.

当前的 XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/">
<xsl:output method="xml" indent="yes"/>

<!-- Variables defined for various input objects, for which the data is matched based on the key to the main object -->
<xsl:variable name="FS_ParentId" select="bpws:getVariableData('FS_ParentId')"/>
<xsl:variable name="FS_ConsultingFirmId" select="bpws:getVariableData('FS_ConsultingFirmId')"/>

<xsl:template match="*:objects">
    <xsl:element name="objects" xmlns="urn:sobject.partner.soap.sforce.com">
        <xsl:for-each select="*:Account">
            <xsl:element name="Account">

                <xsl:variable name="fsparentid" select="*:ParentId"/>
                <xsl:variable name="fsconsultingfirmid" select="*:Consulting_Firm__c"/>

                <xsl:element name="Name">
                    <xsl:value-of select="*:Name"/>
                </xsl:element>
                <xsl:element name="ParentId">
                    <xsl:value-of select="$FS_ParentId/*:objects/*:Account[*:Id = $fsparentid]/*:FinancialServicesID__c"/>
                </xsl:element>
                <xsl:element name="BillingStreet">
                    <xsl:value-of select="*:BillingStreet"/>
                </xsl:element>
                <xsl:element name="BillingState">
                    <xsl:value-of select="*:BillingState"/>
                </xsl:element>
                <xsl:element name="BillingPostalCode">
                    <xsl:value-of select="*:BillingPostalCode"/>
                </xsl:element>
                <xsl:element name="BillingCountry">
                    <xsl:value-of select="*:BillingCountry"/>
                </xsl:element>
                <xsl:element name="BillingCity">
                    <xsl:value-of select="*:BillingCity"/>
                </xsl:element>
                <xsl:element name="Fax">
                    <xsl:value-of select="*:Fax"/>
                </xsl:element>
                <xsl:element name="Website">
                    <xsl:value-of select="*:Website"/>
                </xsl:element>
                <xsl:element name="AnnualRevenue">
                    <xsl:value-of select="*:AnnualRevenue"/>
                </xsl:element>
                <xsl:element name="NumberOfEmployees">
                    <xsl:value-of select="*:NumberOfEmployees"/>
                </xsl:element>
                <xsl:element name="Description">
                    <xsl:value-of select="*:Description"/>
                </xsl:element>
                <xsl:element name="Referenceable__c">
                    <xsl:value-of select="*:Referenceable__c"/>
                </xsl:element>
                <xsl:element name="Company_Type__c">
                    <xsl:value-of select="*:Organization_Type__c"/>
                </xsl:element>
                <xsl:element name="Merge_Direction__c">
                    <xsl:value-of select="*:Merge_Survivor__c"/>
                </xsl:element>
                <xsl:element name="Company_at_Risk__c">
                    <xsl:value-of select="*:At_Risk_Client_Dissatisfaction__c"/>
                </xsl:element>
                <xsl:element name="Consulting_Firm__c">
                    <xsl:value-of select="$FS_ConsultingFirmId/*:objects/*:Account[*:OH_Organization_ID__c = $fsconsultingfirmid]/*:Id"/>
                </xsl:element>
                <xsl:element name="DUNS__c">
                    <xsl:value-of select="*:DUNS__c"/>
                </xsl:element>
                <xsl:element name="Domestic_Ultimate__c">
                    <xsl:value-of select="*:Domestic_Ultimate__c"/>
                </xsl:element>
                <xsl:element name="Global_Ultimate__c">
                    <xsl:value-of select="*:Global_Ultimate__c"/>
                </xsl:element>
                <xsl:element name="HQ_Parent__c">
                    <xsl:value-of select="*:HQ_Parent__c"/>
                </xsl:element>
                <xsl:element name="Legal_Name__c">
                    <xsl:value-of select="*:Legal_Name__c"/>
                </xsl:element>
                <xsl:element name="OH_Organization_ID__c">
                    <xsl:value-of select="*:Id"/>
                </xsl:element>
                <xsl:element name="Original_Effective_Date__c">
                    <xsl:value-of select="*:OHFS_Original_Effective_Date__c"/>
                </xsl:element>
                <xsl:element name="Situs_State__c">
                    <xsl:value-of select="*:Situs_State__c"/>
                </xsl:element>

            </xsl:element>
        </xsl:for-each>
    </xsl:element>
</xsl:template>

从上面的 xsl 输出看起来像

Output from the above xsl looks like

<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com">
<Account>
    <Name>account4</Name>
    <ParentId>001P000000baEuGIAU</ParentId>
    <BillingStreet>124</BillingStreet>
    <BillingState>CA</BillingState>
    <BillingPostalCode>94043</BillingPostalCode>
    <BillingCountry>USA</BillingCountry>
    <BillingCity>san jose</BillingCity>
    <Fax/>
    <Website>test.com</Website>
    <AnnualRevenue/>
    <NumberOfEmployees/>
    <Description/>
    <Referenceable__c/>
    <Company_Type__c>Client</Company_Type__c>
    <Merge_Direction__c/>
    <Company_at_Risk__c>false</Company_at_Risk__c>
    <Consulting_Firm__c>001P000000baKKtIAM</Consulting_Firm__c>
    <DUNS__c/>
    <Domestic_Ultimate__c/>
    <Global_Ultimate__c/>
    <HQ_Parent__c/>
    <Legal_Name__c/>
    <OH_Organization_ID__c>001f000000Er6B5AAJ</OH_Organization_ID__c>
    <Original_Effective_Date__c/>
    <Situs_State__c/>
</Account>

预期输出

<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Account>
    <Name>account4</Name>
    <ParentId>001P000000baEuGIAU</ParentId>
    <BillingStreet>124</BillingStreet>
    <BillingState>CA</BillingState>
    <BillingPostalCode>94043</BillingPostalCode>
    <BillingCountry>USA</BillingCountry>
    <BillingCity>san jose</BillingCity>
    <Fax xsi:nil="true"/>
    <Website>test.com</Website>
    <AnnualRevenue xsi:nil="true"/>
    <NumberOfEmployees xsi:nil="true"/>
    <Description xsi:nil="true"/>
    <Referenceable__c xsi:nil="true"/>
    <Company_Type__c>Client</Company_Type__c>
    <Merge_Direction__c xsi:nil="true"/>
    <Company_at_Risk__c>false</Company_at_Risk__c>
    <Consulting_Firm__c>001P000000baKKtIAM</Consulting_Firm__c>
    <DUNS__c xsi:nil="true"/>
    <Domestic_Ultimate__c xsi:nil="true"/>
    <Global_Ultimate__c xsi:nil="true"/>
    <HQ_Parent__c xsi:nil="true"/>
    <Legal_Name__c xsi:nil="true"/>
    <OH_Organization_ID__c>001f000000Er6B5AAJ</OH_Organization_ID__c>
    <Original_Effective_Date__c xsi:nil="true"/>
    <Situs_State__c xsi:nil="true"/>
</Account>

感谢您的帮助.

这里的输入 XML 和 Account 可能是重复的.

Here's the input XML and Account may be repetitive.

<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Account>
    <Id>001f000000Er6B5AAJ</Id>
    <Website>test.com</Website>
    <Situs_State__c xsi:nil="true"/>
    <Referenceable__c xsi:nil="true"/>
    <ParentId>001f000000DQpQBAA1</ParentId>
    <OwnerId>005a0000009rvNbAAI</OwnerId>
    <Organization_Type__c>Client</Organization_Type__c>
    <Org_PTRR__c>0.0</Org_PTRR__c>
    <Org_PARR__c>0.0</Org_PARR__c>
    <OHFS_Original_Effective_Date__c xsi:nil="true"/>
    <NumberOfEmployees xsi:nil="true"/>
    <Name>account4</Name>
    <Merge_Survivor__c xsi:nil="true"/>
    <Legal_Name__c xsi:nil="true"/>
    <HQ_Parent__c xsi:nil="true"/>
    <Global_Ultimate__c xsi:nil="true"/>
    <FinancialServicesID__c xsi:nil="true"/>
    <Fax xsi:nil="true"/>
    <Estimated_ACR__c>0.0</Estimated_ACR__c>
    <Domestic_Ultimate__c xsi:nil="true"/>
    <Description xsi:nil="true"/>
    <DUNS__c xsi:nil="true"/>
    <Consulting_Firm__c>001f000000Eh6r3AAB</Consulting_Firm__c>
    <BillingStreet>124</BillingStreet>
    <BillingState>CA</BillingState>
    <BillingPostalCode>94043</BillingPostalCode>
    <BillingCountry>USA</BillingCountry>
    <BillingCity>san jose</BillingCity>
    <At_Risk_Client_Dissatisfaction__c>false</At_Risk_Client_Dissatisfaction__c>
    <AnnualRevenue xsi:nil="true"/>
</Account>

推荐答案

由于您的输入 xml 已经在空元素上具有 xsi:nil 属性,您所要做的就是将 <xsl:copy-of select="@xsi:nil"/> 添加到部分生成元素的地方.

Since your input xml already has xsi:nil attribute on empty elements, all you have to do is add <xsl:copy-of select="@xsi:nil"/> to the parts where you generate the elements.

如果结果为空,您可以检查每个元素:

You could check for each element if the result would be empty:

<xsl:element name="Name">
     <xsl:if test="*:Name=''">
         <xsl:attribute name="xsi:nil">true</xsl:attribute>
     </xsl:if>
     <xsl:value-of select="*:Name"/>
</xsl:element>

或者一次性完成所有操作,将帐户写入变量,然后遍历变量并检查是否为空:

or to do it all in one go write the accounts to a variable, than loop over the variable and check if empty:

<xsl:template match="*:objects">
<xsl:element name="objects" xmlns="urn:sobject.partner.soap.sforce.com">
   <!-- write accounts to variable -->
   <xsl:variable name="Accounts">
      <xsl:for-each select="*:Account">
          <xsl:element name="Account">

            <xsl:variable name="fsparentid" select="*:ParentId"/>
            <xsl:variable name="fsconsultingfirmid" select="*:Consulting_Firm__c"/>

            <xsl:element name="Name">
                <xsl:value-of select="*:Name"/>
            </xsl:element>
            
            ...

        </xsl:element>
      
    </xsl:for-each>
  </xsl:variable>

  <xsl:for-each select="$Account">
     <xsl:copy>
        <xsl:for-each select="*">
           <xsl:choose>
              <xsl:when test=".=''">
                 <xsl:copy>
                    <xsl:attribute name="xsi:nil">true</xsl:.attribute>
                 </xsl:copy>
                 <xsl:otherwise>
                    <xsl:copy-of select="."/>
                 </xsl:otherwise>
              </xsl:when>
           </xsl:choose>
        </xsl:for-each>
     </xsl:copy>
  </xsl:for-each>
</xsl:element>
</xsl:template>

这篇关于添加 xsi:nil="true";对于空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆