我正在使用XSLT尝试格式化webservice调用。我能够将“标题”元素复制到翻译的文档,但是我无法复制行项目字段。我正在使用for-each命令,但它不会遍历数据。
我传递的XML看起来像:
<OTC_Alerts_KNA>
<Header>
<current_date_time_of_application_server__c>2015-04-08T12:48:50</current_date_time_of_application_server__c>
<alert_type__c>LSH</alert_type__c>
<single_character_indicator__c>O</single_character_indicator__c>
<alert_functional_area__c>DELV</alert_functional_area__c>
<customer_number__c>1000000118</customer_number__c>
<customer_name__c>WEBCO HAWAII</customer_name__c>
<sales_document_number__c>0000012164</sales_document_number__c>
<sales_document_type__c>ZOR</sales_document_type__c>
</Header>
<Line_Items>
<customer_purchase_order_number__c>LDSHORTAGE11</customer_purchase_order_number__c>
<requested_delivery_date__c>2015-04-01</requested_delivery_date__c>
<material_number__c>000000000100100010</material_number__c>
<short_text_for_sales_order_item__c>SPK RED BRY 12 oz1</short_text_for_sales_order_item__c>
<free_msg_text__c>000010</free_msg_text__c>
<confirmed_quantity__c>10</confirmed_quantity__c>
<pick_quantity__c>10</pick_quantity__c>
</Line_Items>
<Line_Items>
<customer_purchase_order_number__c>LDSHORTAGE11</customer_purchase_order_number__c>
<requested_delivery_date__c>2015-04-01</requested_delivery_date__c>
<material_number__c>000000000100100003</material_number__c>
<short_text_for_sales_order_item__c>RICE KRPS 12CT/CS</short_text_for_sales_order_item__c>
<free_msg_text__c>000020</free_msg_text__c>
<confirmed_quantity__c>10</confirmed_quantity__c>
<pick_quantity__c>10</pick_quantity__c>
</Line_Items>
</OTC_Alerts_KNA>
我的XSLT是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:urn="urn:enterprise.soap.sforce.com" xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<xsl:template match="*">
<xsl:element name="urn:create">
<urn:sObjects xsi:type="urn1:iDoc__c">
<xsl:copy-of select="OTC_Alerts_KNA/Header/node()" />
</urn:sObjects>
<xsl:for-each select="OTC_Alerts_KNA/Line_Items">
<urn:sObjects xsi:type="urn1:iDoc_Line_Item__c">
<xsl:copy-of select="OTC_Alerts_KNA/Line_Items/node()" />
</urn:sObjects>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我得到的输出如下。
<urn:create
xmlns:urn="urn:enterprise.soap.sforce.com">
<urn:sObjects
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xsi:type="urn1:iDoc__c">
<current_date_time_of_application_server__c>2015-04-08T12:48:50</current_date_time_of_application_server__c>
<alert_type__c>LSH</alert_type__c>
<single_character_indicator__c>O</single_character_indicator__c>
<alert_functional_area__c>DELV</alert_functional_area__c>
<customer_number__c>1000000118</customer_number__c>
<customer_name__c>WEBCO HAWAII</customer_name__c>
<sales_document_number__c>0000012164</sales_document_number__c>
<sales_document_type__c>ZOR</sales_document_type__c>
</urn:sObjects>
<urn:sObjects
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xsi:type="urn1:iDoc_Line_Item__c"/>
<urn:sObjects
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xsi:type="urn1:iDoc_Line_Item__c"/>
</urn:create>
如您所见,复制了“OTC_Alerts_KNA / Header”节点,但不会复制订单项。我希望每个“Line_Item”元素及其子元素都被复制。我希望看到的是:
<urn:create xmlns:urn="urn:enterprise.soap.sforce.com">
<urn:sObjects xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="urn1:iDoc__c">
<current_date_time_of_application_server__c>2015-04-08T12:48:50</current_date_time_of_application_server__c>
<alert_type__c>LSH</alert_type__c>
<single_character_indicator__c>O</single_character_indicator__c>
<alert_functional_area__c>DELV</alert_functional_area__c>
<customer_number__c>1000000118</customer_number__c>
<customer_name__c>WEBCO HAWAII</customer_name__c>
<sales_document_number__c>0000012164</sales_document_number__c>
<sales_document_type__c>ZOR</sales_document_type__c>
</urn:sObjects>
<urn:sObjects xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="urn1:iDoc_Line_Item__c">
<customer_purchase_order_number__c>LDSHORTAGE11</customer_purchase_order_number__c>
<requested_delivery_date__c>2015-04-01</requested_delivery_date__c>
<material_number__c>000000000100100010</material_number__c>
<short_text_for_sales_order_item__c>SPK RED BRY 12 oz1</short_text_for_sales_order_item__c>
<free_msg_text__c>000010</free_msg_text__c>
<confirmed_quantity__c>10</confirmed_quantity__c>
<pick_quantity__c>10</pick_quantity__c>
</urn:sObjects>
<urn:sObjects xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="urn1:iDoc_Line_Item__c">
<customer_purchase_order_number__c>LDSHORTAGE11</customer_purchase_order_number__c>
<requested_delivery_date__c>2015-04-01</requested_delivery_date__c>
<material_number__c>000000000100100003</material_number__c>
<short_text_for_sales_order_item__c>RICE KRPS 12CT/CS</short_text_for_sales_order_item__c>
<free_msg_text__c>000020</free_msg_text__c>
<confirmed_quantity__c>10</confirmed_quantity__c>
<pick_quantity__c>10</pick_quantity__c>
</urn:sObjects>
</urn:create>
答案 0 :(得分:3)
你的问题在这里
<xsl:for-each select="OTC_Alerts_KNA/Line_Items">
<!-- context is now at the <Line_Items> node! -->
<urn:sObjects xsi:type="urn1:iDoc_Line_Item__c">
<!-- there is no <OTC_Alerts_KNA> beneath <Line_Items> -->
<xsl:copy-of select="OTC_Alerts_KNA/Line_Items/node()" />
</urn:sObjects>
</xsl:for-each>
您可以使用正确的上下文修复
<xsl:for-each select="OTC_Alerts_KNA/Line_Items">
<urn:sObjects xsi:type="urn1:iDoc_Line_Item__c">
<xsl:copy-of select="node()" />
</urn:sObjects>
</xsl:for-each>
但是,我建议您不要使用<xsl:for-each>
来解决此问题。
使用基于身份模板的转换和一些专门处理一个案例的模板:
<xsl:transform
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:urn="urn:enterprise.soap.sforce.com"
xmlns:urn1="urn:sobject.enterprise.soap.sforce.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="OTC_Alerts_KNA">
<urn:create>
<xsl:apply-templates select="*"/>
</urn:create>
</xsl:template>
<xsl:template match="Header">
<urn:sObjects xsi:type="urn1:iDoc__c">
<xsl:apply-templates select="*"/>
</urn:sObjects>
</xsl:template>
<xsl:template match="Line_Items">
<urn:sObjects xsi:type="urn1:iDoc_Line_Item__c">
<xsl:apply-templates select="*"/>
</urn:sObjects>
</xsl:template>
</xsl:transform>
给你
<urn:create xmlns:urn="urn:enterprise.soap.sforce.com"
xmlns:urn1="urn:sobject.enterprise.soap.sforce.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<urn:sObjects xsi:type="urn1:iDoc__c">
<current_date_time_of_application_server__c>2015-04-08T12:48:50</current_date_time_of_application_server__c>
<alert_type__c>LSH</alert_type__c>
<single_character_indicator__c>O</single_character_indicator__c>
<alert_functional_area__c>DELV</alert_functional_area__c>
<customer_number__c>1000000118</customer_number__c>
<customer_name__c>WEBCO HAWAII</customer_name__c>
<sales_document_number__c>0000012164</sales_document_number__c>
<sales_document_type__c>ZOR</sales_document_type__c>
</urn:sObjects>
<urn:sObjects xsi:type="urn1:iDoc_Line_Item__c">
<customer_purchase_order_number__c>LDSHORTAGE11</customer_purchase_order_number__c>
<requested_delivery_date__c>2015-04-01</requested_delivery_date__c>
<material_number__c>000000000100100010</material_number__c>
<short_text_for_sales_order_item__c>SPK RED BRY 12 oz1</short_text_for_sales_order_item__c>
<free_msg_text__c>000010</free_msg_text__c>
<confirmed_quantity__c>10</confirmed_quantity__c>
<pick_quantity__c>10</pick_quantity__c>
</urn:sObjects>
<urn:sObjects xsi:type="urn1:iDoc_Line_Item__c">
<customer_purchase_order_number__c>LDSHORTAGE11</customer_purchase_order_number__c>
<requested_delivery_date__c>2015-04-01</requested_delivery_date__c>
<material_number__c>000000000100100003</material_number__c>
<short_text_for_sales_order_item__c>RICE KRPS 12CT/CS</short_text_for_sales_order_item__c>
<free_msg_text__c>000020</free_msg_text__c>
<confirmed_quantity__c>10</confirmed_quantity__c>
<pick_quantity__c>10</pick_quantity__c>
</urn:sObjects>
</urn:create>
基于“身份模板”的方法为您提供
<xsl:template match="free_msg_text__c" />
- 完成。)