XSLT转换:适用于每个

时间:2016-06-22 15:40:41

标签: xslt

以下是xslt

的xml输入
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:sf="urn:sobject.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header><LimitInfoHeader><limitInfo><current>4930</current><limit>5000000</limit><type>API REQUESTS</type></limitInfo></LimitInfoHeader></soapenv:Header>
    <soapenv:Body>
        <queryResponse>
            <result xsi:type="QueryResult">
                <done>true</done>
                <queryLocator xsi:nil="true"/>
                <records xsi:type="sf:sObject">
                    <sf:type>Account</sf:type>
                    <sf:Id xsi:nil="true"/>
                    <sf:Contacts xsi:type="QueryResult">
                        <done>true</done>
                        <queryLocator xsi:nil="true"/>
                        <records xsi:type="sf:sObject">
                            <sf:type>Contact</sf:type>
                            <sf:Id>0031700000ThqzkAAB</sf:Id>
                            <sf:Id>0031700000ThqzkAAB</sf:Id>
                        </records>
                        <size>1</size>
                    </sf:Contacts>
                    <sf:Opportunities xsi:type="QueryResult">
                        <done>true</done>
                        <queryLocator xsi:nil="true"/>
                        <records xsi:type="sf:sObject">
                            <sf:type>Opportunity</sf:type>
                            <sf:Id>0061700000BB1kRAAT</sf:Id>
                            <sf:Id>0061700000BB1kRAAT</sf:Id>
                        </records>
                        <size>1</size>
                    </sf:Opportunities>
                </records>
                ***<records xsi:type="sf:sObject">
                    <sf:type>Account</sf:type>
                    <sf:Id xsi:nil="true"/>
                    <sf:Contacts xsi:type="QueryResult">
                        <done>true</done>
                        <queryLocator xsi:nil="true"/>
                        <records xsi:type="sf:sObject">
                            <sf:type>Contact</sf:type>
                            <sf:Id>0031a00000Kdu8zAAB</sf:Id>
                            <sf:Id>0031a00000Kdu8zAAB</sf:Id>
                        </records>
                        <size>1</size>
                    </sf:Contacts>
                    <sf:Opportunities xsi:type="QueryResult">
                        <done>true</done>
                        <queryLocator xsi:nil="true"/>
                        <records xsi:type="sf:sObject">
                            <sf:type>Opportunity</sf:type>
                            <sf:Id>0061a00000Ej6JwAAJ</sf:Id>
                            <sf:Id>0061a00000Ej6JwAAJ</sf:Id>
                        </records>
                        <size>1</size>
                    </sf:Opportunities>
                </records>
                <records xsi:type="sf:sObject">
                    <sf:type>Account</sf:type>
                    <sf:Id xsi:nil="true"/>
                    <sf:Contacts xsi:type="QueryResult">
                        <done>true</done>
                        <queryLocator xsi:nil="true"/>
                        <records xsi:type="sf:sObject">
                            <sf:type>Contact</sf:type>
                            <sf:Id>0031a00000HcNSDAA3</sf:Id>
                            <sf:Id>0031a00000HcNSDAA3</sf:Id>
                        </records>
                        <size>1</size>
                    </sf:Contacts>
                    <sf:Opportunities xsi:type="QueryResult">
                        <done>true</done>
                        <queryLocator xsi:nil="true"/>
                        <records xsi:type="sf:sObject">
                            <sf:type>Opportunity</sf:type>
                            <sf:Id>0061a00000Ej6JDAAZ</sf:Id>
                            <sf:Id>0061a00000Ej6JDAAZ</sf:Id>
                        </records>
                        <size>1</size>
                    </sf:Opportunities>
                </records>***
                <size>3</size>
            </result>
        </queryResponse>
    </soapenv:Body></soapenv:Envelope>
下面的

是xslt代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="urn:partner.soap.sforce.com"
    xmlns:sf="urn:sobject.partner.soap.sforce.com"
    xmlns:sfdc="sfdc">
    <xsl:template match="/">
        <sfdc:sObjects xmlns:sfdc="sfdc" type="OpportunityContactRole">
            <xsl:for-each select="//ns:queryResponse/ns:result/ns:records">
                <xsl:if test="//ns:records/sf:Contacts/ns:done/text() = 'true' and //ns:records/sf:Opportunities/ns:done/text() = 'true'"> 
                <sfdc:sObject>
                    <sfdc:ContactId>
                        <xsl:value-of select="//ns:records/sf:Contacts/ns:records/sf:Id[1]/text()"/>
                    </sfdc:ContactId>
                    <sfdc:IsPrimary>True</sfdc:IsPrimary>
                    <OpportunityId>
                        <xsl:value-of select="//ns:records/sf:Opportunities/ns:records/sf:Id[1]/text()"/>
                    </OpportunityId>
                    <sfdc:Role>Purchaser</sfdc:Role>
                </sfdc:sObject>
               </xsl:if>
           </xsl:for-each>
        </sfdc:sObjects>
    </xsl:template>
</xsl:stylesheet>

下面是我得到的输出

<?xml version="1.0" encoding="utf-8"?>
<sfdc:sObjects xmlns:sfdc="sfdc" xmlns:ns="urn:partner.soap.sforce.com"
    xmlns:sf="urn:sobject.partner.soap.sforce.com" type="OpportunityContactRole">
    <sfdc:sObject>
        <sfdc:ContactId>0031700000ThqzkAAB</sfdc:ContactId>
        <sfdc:IsPrimary>True</sfdc:IsPrimary>
        <OpportunityId>0061700000BB1kRAAT</OpportunityId>
        <sfdc:Role>Purchaser</sfdc:Role>
    </sfdc:sObject>
    <sfdc:sObject>
        <sfdc:ContactId>0031700000ThqzkAAB</sfdc:ContactId>
        <sfdc:IsPrimary>True</sfdc:IsPrimary>
        <OpportunityId>0061700000BB1kRAAT</OpportunityId>
        <sfdc:Role>Purchaser</sfdc:Role>
    </sfdc:sObject>
    <sfdc:sObject>
        <sfdc:ContactId>0031700000ThqzkAAB</sfdc:ContactId>
        <sfdc:IsPrimary>True</sfdc:IsPrimary>
        <OpportunityId>0061700000BB1kRAAT</OpportunityId>
        <sfdc:Role>Purchaser</sfdc:Role>
    </sfdc:sObject>
</sfdc:sObjects>

这里在上面生成的输出中,迭代发生成功,但只考虑了第一个记录值 即0031700000ThqzkAAB和0061700000BB1kRAAT

为什么第二个和第三个记录值(在输入xml中突出显示)确实存在于输出中。无法理解。

总结如下是所需的输出

<?xml version="1.0" encoding="utf-8"?>
<sfdc:sObjects xmlns:sfdc="sfdc" xmlns:ns="urn:partner.soap.sforce.com"
    xmlns:sf="urn:sobject.partner.soap.sforce.com" type="OpportunityContactRole">
    <sfdc:sObject>
        <sfdc:ContactId>0031700000ThqzkAAB</sfdc:ContactId>
        <sfdc:IsPrimary>True</sfdc:IsPrimary>
        <OpportunityId>0061700000BB1kRAAT</OpportunityId>
        <sfdc:Role>Purchaser</sfdc:Role>
    </sfdc:sObject>
    <sfdc:sObject>
        <sfdc:ContactId>0031a00000Kdu8zAAB</sfdc:ContactId>
        <sfdc:IsPrimary>True</sfdc:IsPrimary>
        <OpportunityId>0061a00000Ej6JwAAJ</OpportunityId>
        <sfdc:Role>Purchaser</sfdc:Role>
    </sfdc:sObject>
    <sfdc:sObject>
        <sfdc:ContactId>0031a00000HcNSDAA3</sfdc:ContactId>
        <sfdc:IsPrimary>True</sfdc:IsPrimary>
        <OpportunityId>0061a00000Ej6JDAAZ</OpportunityId>
        <sfdc:Role>Purchaser</sfdc:Role>
    </sfdc:sObject>
</sfdc:sObjects>

请求帮我输出

2 个答案:

答案 0 :(得分:0)

当您进入xsl:for-each时,上下文为//ns:queryResponse/ns:result/ns:records

当您执行xsl:value-of时,您将从文档开头的//开始选择。尝试相对于当前上下文进行选择...

  <xsl:template match="/">
    <sfdc:sObjects xmlns:sfdc="sfdc" type="OpportunityContactRole">
      <xsl:for-each select="//ns:queryResponse/ns:result/ns:records">
        <xsl:if test="sf:Contacts/ns:done = 'true' and sf:Opportunities/ns:done = 'true'"> 
          <sfdc:sObject>
            <sfdc:ContactId>
              <xsl:value-of select="sf:Contacts/ns:records/sf:Id[1]"/>
            </sfdc:ContactId>
            <sfdc:IsPrimary>True</sfdc:IsPrimary>
            <OpportunityId>
              <xsl:value-of select="sf:Opportunities/ns:records/sf:Id[1]"/>
            </OpportunityId>
            <sfdc:Role>Purchaser</sfdc:Role>
          </sfdc:sObject>
        </xsl:if>
      </xsl:for-each>
    </sfdc:sObjects>
  </xsl:template>

编辑:您还应该在xsl:if中创建相对于当前上下文的xpath。更新了上面的模板。

您还可以将测试移至xsl:for-each中的谓词并完全删除xsl:if ...

<xsl:for-each select="//ns:queryResponse/ns:result/ns:records[sf:Contacts/ns:done = 'true' and sf:Opportunities/ns:done = 'true']">
  <sfdc:sObject>
      ...
  </sfdc:sObject>
</xsl:for-each>

答案 1 :(得分:0)

修改您的xpath://ns:records/sf:Contacts/ns:records/sf:Id[1]/text()以删除//ns:records

所以它只是sf:Contacts/ns:records/sf:Id[1]/text()

//ns:records/sf:Opportunities/ns:records/sf:Id[1]/text()sf:Opportunities/ns:records/sf:Id[1]/text()