简体   繁体   中英

How do I convert an XML complextype element to individual element for XML transormation using XSLT 1.0

I have an XML document below:

<?xml version="1.0" encoding="UTF-8"?>
<Response_Data>
<Purchase_Order>
     <Purchase_Order_Data>
        <Purchase_Order_ID>PURCHASE_ORDER-6-1</Purchase_Order_ID>
        <Locked_in_Workday>0</Locked_in_Workday>
        <Document_Number>PO-1000292</Document_Number>
        <Supplier_Reference Descriptor="Cardinal Health">
           <ID type="WID">777d89f438960138f522dd70ce00d3ae</ID>
           <ID type="Supplier_Reference_ID">SH199</ID>
           <ID type="Supplier_ID">S-1725</ID>
        </Supplier_Reference>
        <Document_Date>2020-03-30</Document_Date>
        <Acknowledgement_Expected>0</Acknowledgement_Expected>
        <Auto_Sourced>0</Auto_Sourced>
        <Tax_Amount>0</Tax_Amount>
        <Freight_Amount>0</Freight_Amount>
        <Other_Charges>0</Other_Charges>
     </Purchase_Order_Data>
  </Purchase_Order>
  <Purchase_Order>
     <Purchase_Order_Data>
        <Purchase_Order_ID>PURCHASE_ORDER-6-2</Purchase_Order_ID>
        <Locked_in_Workday>0</Locked_in_Workday>
        <Document_Number>PO-1000293</Document_Number>
        <Supplier_Reference Descriptor="Medtronic Inc">
           <ID type="WID">777d89f43896016d93c653c1ce0035f9</ID>
           <ID type="Supplier_Reference_ID">SH36</ID>
           <ID type="Supplier_ID">S-2935</ID>
        </Supplier_Reference>
        <Document_Date>2020-03-30</Document_Date>
        <Acknowledgement_Expected>0</Acknowledgement_Expected>
        <Auto_Sourced>0</Auto_Sourced>
        <Tax_Amount>0</Tax_Amount>
        <Freight_Amount>0</Freight_Amount>
        <Other_Charges>0</Other_Charges>
     </Purchase_Order_Data>
  </Purchase_Order>
</Response_Data>

I want to convert this to:

<?xml version="1.0" encoding="UTF-8"?>
<Response_Data>
  <Purchase_Order>
     <Purchase_Order_Data>
        <Purchase_Order_ID>PURCHASE_ORDER-6-1</Purchase_Order_ID>
        <Locked_in_Workday>0</Locked_in_Workday>
        <Document_Number>PO-1000292</Document_Number>
        <Supplier_Reference>
            <Supplier_Name>Cardinal Health</Supplier_Name>
            <Supplier_ID>S-1725</Supplier_ID>
        </Supplier_Reference>
        <Document_Date>2020-03-30</Document_Date>
        <Acknowledgement_Expected>0</Acknowledgement_Expected>
        <Auto_Sourced>0</Auto_Sourced>
        <Tax_Amount>0</Tax_Amount>
        <Freight_Amount>0</Freight_Amount>
        <Other_Charges>0</Other_Charges>
     </Purchase_Order_Data>
  </Purchase_Order>
  <Purchase_Order>
     <Purchase_Order_Data>
        <Purchase_Order_ID>PURCHASE_ORDER-6-2</Purchase_Order_ID>
        <Locked_in_Workday>0</Locked_in_Workday>
        <Document_Number>PO-1000293</Document_Number>
        <Supplier_Reference>
            <Supplier_Name>Medtronic Inc</Supplier_Name>
            <Supplier_ID>S-2935</Supplier_ID>
        </Supplier_Reference>
        <Document_Date>2020-03-30</Document_Date>
        <Acknowledgement_Expected>0</Acknowledgement_Expected>
        <Auto_Sourced>0</Auto_Sourced>
        <Tax_Amount>0</Tax_Amount>
        <Freight_Amount>0</Freight_Amount>
        <Other_Charges>0</Other_Charges>
     </Purchase_Order_Data>
  </Purchase_Order>
</Response_Data>

The following is the XSLT I am using.

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

    <xsl:template match="/">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Supplier_Reference">
        <Supplier_Reference>
            <Purchase_Order_ID><xsl:value-of select="../Purchase_Order_ID"/></Purchase_Order_ID>
            <Supplier_ID><xsl:value-of select="ID"/></Supplier_ID>
            <Supplier_Name><xsl:value-of select="../Supplier_Reference/attribute::Descriptor"/> 
        </Supplier_Name>
        </Supplier_Reference>
    </xsl:template>

</xsl:stylesheet>

The issue I am having is the output is using the first Supplier_Reference/ID that is available. I want to output the Supplier_Reference/ID of type "Supplier_ID". How do I reference this complex type in the XSLT docuemtn?

Change:

<xsl:value-of select="ID"/>

to:

<xsl:value-of select="ID[@type='Supplier_ID']"/>

Note that your 1st template is redundant; the built-in template rules already do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM