Home > front end >  XSLT How to combine templates and add an existing field to certain elements
XSLT How to combine templates and add an existing field to certain elements

Time:11-09

I'm trying to change the xml file, have tried several times of this but unable to get any meaningful output. The transfored xml file should be :

  1. without mask attribute
  2. version and status fields should be deleted from all nodes
  3. field Number must be top field of Record, element1, element2, element etc.

Any help would be greatly appreciated

This is my xml input :

    <Objects>
        <Record mask="1234qwer">
            <reference>104006ALS</reference>
            <version>9</version>
            <status>NEW</status>
            <createdTime>2018-06-18T09:37:48.560Z</createdTime>
            <links>
                <link mask="1234qwer12">
                    <reference>112045MS</reference>
                    <version>0</version>
                    <linkReason>Revision</linkReason>
                    <createdDateTime>2018-07-30T10:45:16.870Z</createdDateTime>
                    <lastModifiedDateTime>2018-07-30T10:45:16.870Z</lastModifiedDateTime>
                    <relevantDateTime>2018-07-30T10:45:16.870Z</relevantDateTime>
                    <element1 mask="eqwrwqe12">
                        <reference>17001</reference>
                        <version>1</version>
                    </element1>
                </link>
                <link mask="q0SM9">
                    <reference>109085</reference>
                    <version>0</version>
                    <linkReason>SomeReason</linkReason>
                    <createdDateTime>2018-05-18T10:50:08.520Z</createdDateTime>
                    <lastModifiedDateTime>2018-05-18T10:50:08.520Z</lastModifiedDateTime>
                    <relevantDateTime>2018-05-18T10:50:08.520Z</relevantDateTime>
                    <element2 mask="q0SM934232">
                        <reference>13001</reference>
                        <version>1</version>
                        <conditions>
                            <condition mask="q0SM921312">
                                <reference>9001</reference>
                                <version>1</version>
                                <conditionNumber>1</conditionNumber>
                                <conditionVariations/>
                            </condition>
                        </conditions>
                    </element2>
                </link>
                <link mask="yq0SM92">
                    <reference>109043</reference>
                    <version>0</version>
                    <element3 mask="yJq0SM92">
                        <reference>21006</reference>
                        <version>8</version>
                        <status>NEW</status>
                        <createdDateTime>2018-05-18T10:16:22.430Z</createdDateTime>
                        <links>
                            <link mask="yJq0SM9212">
                                <reference>112193</reference>
                                <version>1</version>
                                <fromDate>2018-07-31</fromDate>
                                <element4 mask="12yJq0SM9">
                                    <reference>26004</reference>
                                    <version>0</version>
                                    <status>NEW</status>
                                    <createdDateTime>2018-08-24T20:46:55.500Z</createdDateTime>
                                </element4>
                            </link>
                        </links>
                    </element3>
                </link>
            </links>
            <Number>999999</Number>
        </Record>
    </Objects>
</AllObjects>

Current xslt :

  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @* "/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="@mask"/>
  
  <xsl:template match="version | status"/>
  
  <xsl:template match="Record | Record/links/link/element1 | Record/links/link/element2 | Record/links/link/element3 | Record/links/link/element4">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="Number"/>
      <xsl:apply-templates/> 
        </xsl:copy>
      </xsl:template>

  
  </xsl:stylesheet> 

Desired Result :

    <Objects>
        <Record>
            <Number>999999</Number>
            <reference>104006ALS</reference>
            <createdTime>2018-06-18T09:37:48.560Z</createdTime>
            <links>
                <link>
                    <reference>112045MS</reference>
                    <linkReason>Revision</linkReason>
                    <createdDateTime>2018-07-30T10:45:16.870Z</createdDateTime>
                    <lastModifiedDateTime>2018-07-30T10:45:16.870Z</lastModifiedDateTime>
                    <relevantDateTime>2018-07-30T10:45:16.870Z</relevantDateTime>
                    <element1>
                        <Number>999999</Number>
                        <reference>17001</reference>
                    </element1>
                </link>
                <link>
                    <reference>109085</reference>
                    <linkReason>SomeReason</linkReason>
                    <createdDateTime>2018-05-18T10:50:08.520Z</createdDateTime>
                    <lastModifiedDateTime>2018-05-18T10:50:08.520Z</lastModifiedDateTime>
                    <relevantDateTime>2018-05-18T10:50:08.520Z</relevantDateTime>
                    <element2>
                        <Number>999999</Number>
                        <reference>13001</reference>
                        <conditions>
                            <condition>
                                <reference>9001</reference>
                                <conditionNumber>1</conditionNumber>
                                <conditionVariations/>
                            </condition>
                        </conditions>
                    </element2>
                </link>
                <link>
                    <reference>109043</reference>
                    <element3>
                        <Number>999999</Number>
                        <reference>21006</reference>
                        <createdDateTime>2018-05-18T10:16:22.430Z</createdDateTime>
                        <links>
                            <link>
                                <reference>112193</reference>
                                <fromDate>2018-07-31</fromDate>
                                <element4>
                                    <Number>999999</Number>
                                    <reference>26004</reference>
                                    <createdDateTime>2018-08-24T20:46:55.500Z</createdDateTime>
                                </element4>
                            </link>
                        </links>
                    </element4>
                </link>
            </links>
            <Number>999999</Number>
        </Record>
    </Objects>
</AllObjects>

CodePudding user response:

You're almost there - try:

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" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="@mask | version | status"/>

<xsl:template match="Record | element1 | element2 | element3 | element4">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:copy-of select="ancestor-or-self::Record/Number"/>
        <xsl:apply-templates/> 
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

If (as it seems from your example) there will be no attributes other than mask, you could reduce the code to:

<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" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="*">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="version | status"/>

<xsl:template match="Record | element1 | element2 | element3 | element4">
    <xsl:copy>
        <xsl:copy-of select="ancestor-or-self::Record/Number"/>
        <xsl:apply-templates/> 
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
  • Related