xslt - How to Duplicate xml elements

Xslt - How to Duplicate xml elements

To duplicate XML elements using XSLT, you can use a combination of the xsl:copy-of and xsl:for-each elements. Here's a basic example:

Let's say you have the following XML input:

<root> <item>Item 1</item> <item>Item 2</item> </root> 

And you want to duplicate each <item> element. You can use the following XSLT transformation to achieve this:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- Duplicate item elements --> <xsl:template match="item"> <xsl:copy-of select="."/> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet> 

This XSLT stylesheet performs an identity transform, which copies all nodes and attributes unchanged, and then provides a specific template to match <item> elements. Within this template, we use xsl:copy-of twice to duplicate each <item> element.

When you apply this XSLT transformation to the input XML, you'll get the following output:

<root> <item>Item 1</item> <item>Item 1</item> <item>Item 2</item> <item>Item 2</item> </root> 

Each <item> element is duplicated in the output XML. Adjust the XSLT template according to your specific requirements and XML structure.

Examples

  1. How to duplicate XML elements in XSLT?

    • Description: This query seeks methods to duplicate XML elements using XSLT, often required in scenarios like XML transformation or data manipulation.
    <!-- Example: Duplicating XML elements using XSLT --> <xsl:template match="element"> <xsl:copy-of select="."/> <xsl:copy-of select="."/> </xsl:template> 
  2. XSLT to clone XML nodes or elements?

    • Description: This query aims to find XSLT code snippets to clone or duplicate XML nodes or elements, commonly needed in XML processing tasks.
    <!-- Example: Cloning XML nodes using XSLT --> <xsl:template match="node"> <xsl:copy-of select="."/> <xsl:copy-of select="."/> </xsl:template> 
  3. How to create copies of XML elements using XSLT?

    • Description: This query looks for techniques to create duplicates or copies of XML elements using XSLT for various XML manipulation tasks.
    <!-- Example: Creating copies of XML elements using XSLT --> <xsl:template match="element"> <xsl:copy-of select="."/> <xsl:copy-of select="."/> </xsl:template> 
  4. XSLT code to duplicate XML elements or nodes?

    • Description: This query seeks XSLT code examples to duplicate XML elements or nodes, commonly used in XSLT transformations and XML processing.
    <!-- Example: Duplicating XML elements or nodes using XSLT --> <xsl:template match="node"> <xsl:copy-of select="."/> <xsl:copy-of select="."/> </xsl:template> 
  5. How to repeat or replicate XML elements in XSLT?

    • Description: This query aims to find methods to repeat or replicate XML elements multiple times within an XSLT stylesheet.
    <!-- Example: Repeating XML elements in XSLT --> <xsl:template match="element"> <xsl:copy-of select=". | . | ."/> </xsl:template> 
  6. XSLT to generate multiple copies of XML elements?

    • Description: This query looks for XSLT code snippets to generate multiple copies of XML elements or nodes as required.
    <!-- Example: Generating multiple copies of XML elements using XSLT --> <xsl:template match="element"> <xsl:copy-of select=". | . | ."/> </xsl:template> 
  7. How to clone XML elements with XSLT and add them to the output?

    • Description: This query seeks techniques to clone XML elements using XSLT and include them in the output document for various XML transformation needs.
    <!-- Example: Cloning XML elements with XSLT --> <xsl:template match="element"> <xsl:copy-of select=". | ."/> </xsl:template> 
  8. XSLT to duplicate specific XML elements based on conditions?

    • Description: This query aims to find XSLT code examples to duplicate specific XML elements based on certain conditions or criteria.
    <!-- Example: Duplicating specific XML elements based on conditions using XSLT --> <xsl:template match="element"> <xsl:if test="condition"> <xsl:copy-of select=". | ."/> </xsl:if> </xsl:template> 
  9. How to create multiple copies of XML elements in XSLT with dynamic content?

    • Description: This query looks for methods to create multiple copies of XML elements in XSLT while allowing dynamic content generation.
    <!-- Example: Creating multiple copies of XML elements with dynamic content using XSLT --> <xsl:template match="element"> <xsl:for-each select="1 to 3"> <xsl:copy-of select="."/> </xsl:for-each> </xsl:template> 
  10. XSLT code to duplicate XML elements conditionally and add them to the output?

    • Description: This query seeks XSLT code snippets to duplicate XML elements conditionally based on certain criteria and include them in the output document.
    <!-- Example: Duplicating XML elements conditionally with XSLT --> <xsl:template match="element"> <xsl:if test="condition"> <xsl:copy-of select=". | ."/> </xsl:if> </xsl:template> 

More Tags

user-agent full-text-indexing ms-office dojo-1.8 electron-packager printstacktrace selenium-rc discord.py dalvik flutter-navigation

More Programming Questions

More Bio laboratory Calculators

More Mortgage and Real Estate Calculators

More Stoichiometry Calculators

More Investment Calculators