简体   繁体   中英

xml transformation using xslt 2.0

I'm trying to do SOAP to XML transformation in XSLT 2.0, this is the source xml:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xi="http://www.xcdm.com">
<soapenv:Header/>
<soapenv:Body>
    <xi:root>
        <xmlHeader>
            <timestamp>1232135468</timestamp>
            <source>xds unit</source>
        </xmlHeader>
        <xmlBody>
            <method>create</method>
            <customer>
                <custA>VOC-Prov</custA>
                <custB>vocprov</custB>
            </customer>
            <Item>
                <subElmt1>12345</subElmt1>
                <subElmt2>534321</subElmt2>
            </Item>
        </xmlBody>
    </xi:root>
</soapenv:Body>

`

I have been looking for days for a way to transform the structure using XSLT 2.0 so that the output will structured by this one :

    <customer version="1.0">
    <custA>VOC-Prov</custA>
    <custB>vocprov</custB>
        <Item name="constName" method="value from the  method element (create)">
        <Attribute name="subElmt1">value of subElmt1 element</Attribute>
        <Attribute name="subElmt2">534321</Attribute>
    </Item>
</customer>
  1. How can i Concat the method value as attribute in the requested xml result ?
  2. What is the best way to implement xslt that convert element to attribute that match this xml hirarchiel ?

Any help on this would be greatly appreciated!

I have been looking for days for a way to transform the structure

Not sure why you're having such trouble with this, as it seems like a relatively easy task:

XSLT 2.0

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

<xsl:template match="/">
    <customer version="1.0">
        <xsl:variable name="body" select="//xmlBody" />
        <xsl:copy-of select="$body/customer/*" copy-namespaces="no"/>
        <Item name="constName" method="{$body/method}">
            <xsl:for-each select="$body/Item/*">
                <Attribute name="{name()}">
                    <xsl:value-of select="." />
                </Attribute>
            </xsl:for-each>
        </Item>
    </customer>
</xsl:template>

</xsl:stylesheet>

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