Copy Nodes and Children Using XSLT

2019-03-06 20:04发布

问题:

This seems like a pretty basic XSLT question, but I have not been able to find the answer.

My XSLT is:

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

I would like to copy all nodes under ProcessData/Document. Currently, this only copies the values. I would like to copy all the elements under 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>

I am trying to get this in the end:

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

Is there a way to do that?

Thanks.

回答1:

If you are sure that's the output you want, you can achieve it very easily by:

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>


标签: xml xslt