move element from an xml file to another xml element using xslt-3

user9413573

Given the following example xml file:

<?xml version="1.0"?>
        <catalog>
           <book id="bk101">
              <author>Gambardella, Matthew</author>
              <title>XML Developer's Guide</title>
              <genre>Computer</genre>
              <price>44.95</price>
              <publish_date>2000-10-01</publish_date>
              <description>An in-depth look at creating applications 
              with XML.</description>
           </book>
           <book id="bk102">
              <author>Ralls, Kim</author>
              <title>Midnight Rain</title>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-12-16</publish_date>
              <description>A former architect battles corporate zombies, 
              an evil sorceress, and her own childhood to become queen 
              of the world.</description>
           </book>
           <book id="bk103">
              <title>Maeve Ascendant</title>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-11-17</publish_date>
              <description>After the collapse of a nanotechnology 
              society in England, the young survivors lay the 
              foundation for a new society.</description>
           </book>
        </catalog>

I need to merge all title elements into the author element, by a rule: If the author element already exists, then the title element's value will be inserted in the author record, using a separator ie a | character. If there is not an author element in the record, the title element would become the author element. Desired output:

 <?xml version="1.0"?>
        <catalog>
           <book id="bk101">
              <author>Gambardella, Matthew | XML Developer's Guide</author>
              <genre>Computer</genre>
              <price>44.95</price>
              <publish_date>2000-10-01</publish_date>
              <description>An in-depth look at creating applications 
              with XML.</description>
           </book>
           <book id="bk102">
              <author>Ralls, Kim | Midnight Rain</author>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-12-16</publish_date>
              <description>A former architect battles corporate zombies, 
              an evil sorceress, and her own childhood to become queen 
              of the world.</description>
           </book>
           <book id="bk103">
              <author>Maeve Ascendant</author>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-11-17</publish_date>
              <description>After the collapse of a nanotechnology 
              society in England, the young survivors lay the 
              foundation for a new society.</description>
           </book>
        </catalog>

How the above result could be achieved using xslt-3?

Daniel Haley

What I would do is strip the existing author element and replace the title element with a new author element.

The value of the new author element would be a string-join of the original author (if it exists) and the current title value separated by |.

Example...

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

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="author"/>

  <xsl:template match="title">
    <author>
      <xsl:value-of select="string-join((../author,.),' | ')"/>
    </author>
  </xsl:template>

</xsl:stylesheet>

Note: This would work as XSLT 2.0 too if you replaced xsl:mode with the identity transform.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inserting nodes from a XML to another XML file using xslt

From Dev

Changing the value of an attribute of the document element in an xml file using XSLT

From Dev

Create elements in target XML using data from source XML as element name using XSLT

From Dev

Referencing an XML attribute from another xml element

From Dev

Change XML element name using XSLT

From Dev

Move attributes from one xml file to another via xslt

From Dev

Trouble Inserting an element into xml using XSLT 2.0

From Dev

Move XML element using XDT

From Dev

Removing any element with a certain value from XML using XSLT

From Dev

How to remove a specific element from XML, using XSLT

From Dev

How to insert new element in the xml using XSLT

From Dev

Update XML Parent Element Attribute Using XSLT

From Dev

Referencing an XML attribute from another xml element

From Dev

Modifying XML element using Javascript (in XSLT stylesheet)

From Dev

Delete a tag from XML file using XSLT if another tag is empty

From Dev

Move XML element using XDT

From Dev

copying an element from 1 xml to another using xsl 2.0

From Dev

How to insert new element in the xml using XSLT

From Dev

How to extract element names from a XML Schema using XSLT?

From Dev

Finding a specific element in a xml file using xslt

From Dev

Move an XML element from its place, to under another parent element using xslt

From Dev

How to remove unnecessary attributes from element in XML using XSLT?

From Dev

XSLT copy elements from xml by element match

From Dev

Move tag to child element xml - xslt

From Dev

How to construct xml from another xml using xslt3.0 where xml attribute values should be the new element names

From Dev

Changing XML element tag using XSLT

From Dev

How to format string in XML element using xslt

From Dev

Get Element from another XML file, based on an element value from current XML

From Dev

make xml element unique using xslt-3

Related Related

  1. 1

    Inserting nodes from a XML to another XML file using xslt

  2. 2

    Changing the value of an attribute of the document element in an xml file using XSLT

  3. 3

    Create elements in target XML using data from source XML as element name using XSLT

  4. 4

    Referencing an XML attribute from another xml element

  5. 5

    Change XML element name using XSLT

  6. 6

    Move attributes from one xml file to another via xslt

  7. 7

    Trouble Inserting an element into xml using XSLT 2.0

  8. 8

    Move XML element using XDT

  9. 9

    Removing any element with a certain value from XML using XSLT

  10. 10

    How to remove a specific element from XML, using XSLT

  11. 11

    How to insert new element in the xml using XSLT

  12. 12

    Update XML Parent Element Attribute Using XSLT

  13. 13

    Referencing an XML attribute from another xml element

  14. 14

    Modifying XML element using Javascript (in XSLT stylesheet)

  15. 15

    Delete a tag from XML file using XSLT if another tag is empty

  16. 16

    Move XML element using XDT

  17. 17

    copying an element from 1 xml to another using xsl 2.0

  18. 18

    How to insert new element in the xml using XSLT

  19. 19

    How to extract element names from a XML Schema using XSLT?

  20. 20

    Finding a specific element in a xml file using xslt

  21. 21

    Move an XML element from its place, to under another parent element using xslt

  22. 22

    How to remove unnecessary attributes from element in XML using XSLT?

  23. 23

    XSLT copy elements from xml by element match

  24. 24

    Move tag to child element xml - xslt

  25. 25

    How to construct xml from another xml using xslt3.0 where xml attribute values should be the new element names

  26. 26

    Changing XML element tag using XSLT

  27. 27

    How to format string in XML element using xslt

  28. 28

    Get Element from another XML file, based on an element value from current XML

  29. 29

    make xml element unique using xslt-3

HotTag

Archive