使用XSLT复制节点和子节点

时间:2015-04-07 19:12:06

标签: xml xslt

这似乎是一个非常基本的XSLT问题,但我找不到答案。

我的XSLT是:

<xsl:template match="ProcessData/Document" >
    <xsl:element name="urn:create">
   <urn:sObjects xsi:type="urn1:O2C_Alert__c">
    <xsl:copy>
    <xsl:apply-templates select="ProcessData/Document" />
   </xsl:copy>
</urn:sObjects>

我想复制ProcessData / Document下的所有节点。目前,这仅复制值。我想复制ProcessData / Document下的所有元素。

<ProcessData>
  <Document>
    <OTC_Alerts_KNA>
<Header><current_date_time_of_application_server>2015-03-31T21:56:51</current_date_time_of_application_server>
        <alert_type>EDI</alert_type>
        <single_character_indicator>O</single_character_indicator>
        <alert_functional_area>ORD</alert_functional_area>
        <customer_number>1000000131</customer_number>
        <customer_name>Dot Foods</customer_name>
        <sales_document_number>0000012062</sales_document_number>
        <sales_document_type>ZOR</sales_document_type>
    </Header>
    </OTC_Alerts_KNA>
  </Document>
</ProcessData>

我试图最终得到这个:

<?xml version="1.0" encoding="UTF-8"?>
<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:O2C_Alert__c"/>
                <OTC_Alerts_KNA>
    <Header><current_date_time_of_application_server>2015-03-31T21:56:51</current_date_time_of_application_server>
            <alert_type>EDI</alert_type>
            <single_character_indicator>O</single_character_indicator>
            <alert_functional_area>ORD</alert_functional_area>
            <customer_number>1000000131</customer_number>
            <customer_name>Dot Foods</customer_name>
            <sales_document_number>0000012062</sales_document_number>
            <sales_document_type>ZOR</sales_document_type>
        </Header>
        </OTC_Alerts_KNA>
    </urn:create>

有办法吗?

感谢。

1 个答案:

答案 0 :(得分:1)

如果您确定这是您想要的输出,您可以通过以下方式轻松实现:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <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:O2C_Alert__c"/>
        <xsl:copy-of select="ProcessData/Document/*"/>
    </urn:create>
</xsl:template>

</xsl:stylesheet>