Generating XML from xpath using xslt

2020-05-07 02:04发布

问题:

I need to generate xml on the fly for a soapUI project I am working on. The approach I think works best from a user perspective is having data in excel like the image below.

And maintaining a xpath to column name map using a template XML which looks something like this....

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="URL1" xmlns:ns1="URL2" xmlns:ns2="URL3" xmlns:ns3="URL4" xmlns:ns4="URL5" xmlns:ns5="URL6">
    <soap:Header/>
    <soap:Body>
        <ns:Node1>
            <Node2>
                <ns1:Node3>
                    <ns4:btc>
                        <ns5:AnotherNode>
                            <ns2:ownerDN>Value1</ns2:ownerDN>
                            <ns2:context>
                                <!--one or more occurrences-->
                                <ns2:contextItem name="Attribute1">value2</ns2:contextItem>
                            </ns2:context>
                            <ns2:type>Value5</ns2:type>
                            <ns2:value>Value6</ns2:value>
                        </ns5:AnotherNode>
                        <ns5:role>value7</ns5:role>
                    </ns4:btc>
                </ns1:Node3>
            </Node2>
        </ns:Node1>
    </soap:Body>
</soap:Envelope>

xpath 2 col name map

I am using a XSLT from one of the answers on SO(sorry I am unable to find that answer now) to generate the xpaths.

What i want to do now is generate an XML like the one below based on the xpaths. For element/attribute values I will be using the values in the excel. I would prefer for this to be done using an XSLT but i am not good at XSLT.

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:ns="URL1"
               xmlns:ns1="URL2"
               xmlns:ns2="URL3"
               xmlns:ns3="URL4"
               xmlns:ns4="URL5"
               xmlns:ns5="URL6">
   <soap:Header/>
   <soap:Body>
      <ns:Node1>
         <Node2>
            <ns1:Node3>
               <ns4:btc>
                  <ns5:AnotherNode>
                     <ns2:ownerDN>Value1</ns2:ownerDN>
                     <ns2:context>
                        <ns2:contextItem name="Attribute1">value2</ns2:contextItem>
                        <ns2:contextItem name="Attribute2">value3</ns2:contextItem>
                        <ns2:contextItem name="Attribute3">value4</ns2:contextItem>
                     </ns2:context>
                     <ns2:type>Value5</ns2:type>
                     <ns2:value>Value6</ns2:value>
                  </ns5:AnotherNode>
                  <ns5:role>value7</ns5:role>
               </ns4:btc>
                  </ns1:Node3>
               </Node2>
         </ns:Node1>
      </soap:Body>
</soap:Envelope>

Lastly, even though i did mention soapUI, this solution needs to be implemented in excel using VBA.