tarek sherif
tarek sherif

Reputation: 1

Setting a sibling node to be a parent in xml using xslt

I need to transform the following input xml to the needed output format as shown below using xslt, How can I set <BTAT_MEASUREMENTSet> to be the Parent of as the needed output using xslt ?, it's always the at the second level of the message after the root so the relative postion from the root is constant

**Input:**

    <?xml version="1.0" encoding="UTF-8"?>
<SyncBTAT_MEASUREMENT
    xmlns="http://www.llll.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2021-03-24T15:39:55+00:00" transLanguage="EN" baseLanguage="EN" messageID="176416166003958143562">
    <BTAT_MEASUREMENTSet></BTAT_MEASUREMENTSet>
    <MEASUREPOINT action="AddChange">
        <BTATMBO>MEASUREMENT</BTATMBO>
        <ORGID>TAL3-DE</ORGID>
        <POINTNUM>TEMP</POINTNUM>
        <SITEID>TAL3</SITEID>
        <MEASUREMENT>
            <ASSETNUM>2403871\PPC</ASSETNUM>
            <MEASUREDATE>2020-06-16T06:47:07+00:00</MEASUREDATE>
            <MEASUREMENTID/>
            <MEASUREMENTVALUE>20.0</MEASUREMENTVALUE>
            <METERNAME>TIME-MIN-001</METERNAME>
        </MEASUREMENT>
    </MEASUREPOINT>
</SyncBTAT_MEASUREMENT>

**Needed Output:**

    <?xml version="1.0" encoding="UTF-8"?>
<SyncBTAT_MEASUREMENT
    xmlns="http://www.llll.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2021-03-24T15:39:55+00:00" transLanguage="EN" baseLanguage="EN" messageID="176416166003958143562">
 <BTAT_MEASUREMENTSet>
    <MEASUREPOINT action="AddChange">
        <BTATMBO>MEASUREMENT</BTATMBO>
        <ORGID>TAL3-DE</ORGID>
        <POINTNUM>TEMP</POINTNUM>
        <SITEID>TAL3</SITEID>
        <MEASUREMENT>
            <ASSETNUM>2403871\PPC</ASSETNUM>
            <MEASUREDATE>2020-06-16T06:47:07+00:00</MEASUREDATE>
            <MEASUREMENTID/>
            <MEASUREMENTVALUE>20.0</MEASUREMENTVALUE>
            <METERNAME>TIME-MIN-001</METERNAME>
        </MEASUREMENT>
    </MEASUREPOINT>
  </BTAT_MEASUREMENTSet>
</SyncBTAT_MEASUREMENT>

Upvotes: 0

Views: 46

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

If you know that the name of the element you want to be the parent starts with BTAT_, and that it has no sibling element whose name also starts with BTAT_, then you could do:

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:strip-space elements="*"/>

<xsl:template match="/*">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="*[starts-with(local-name(), 'BTAT')]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*">
    <xsl:copy>
        <xsl:copy-of select="../* except self::*"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

This is assuming you don't know the name of any elements in the input XML, incl. the root element - otherwise it could be simpler.

Upvotes: 1

Related Questions