添加xsi:nil =" true"对于空元素

时间:2014-03-27 14:58:19

标签: xml xslt-2.0

我正试图想办法为空元素添加xsi:nil =“true”。我可以在输出上写一个单独的xsl来实现这个目的,但是试着弄清楚我是否可以用当前的xsl编写它。

当前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的输出类似于

<?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和帐户可能重复。

<?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>

1 个答案:

答案 0 :(得分:2)

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


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

<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>

或者一个接一个地将帐户写入变量,而不是循环变量并检查是否为空:

<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>