XML CSV JSON Equivalences - sgml/signature GitHub Wiki

Original:

<Parent Name="name2">
    <Child Name="ChildName1"></Child>
    <Child Name="ChildName2"></Child>
</Parent>

Minimized XML:

<names>
     <name1>
         <childName1/>
         <childName2/>
     </name1>
     <name2>
         <childName1/>
         <childName2/>
     </name2>
</names>

And the CSV to this:

name1  ChildName1
name1  ChildName2
name2  ChildName1
name2  ChildName2

with a JSON serialization like this:

{"names":
 [
  {
  "name1":
   [
    {
    "childName1":""
    },
    {
    "childName2":""
    }
   ]
  },
  {
  "name2":
   [
    {
    "childName1":""
    },
    {
    "childName2":""
    }
   ]
  }
 ]
}

In a row-oriented fashion, the XML could be:

<names>
    <name1 name="ChildName1|ChildName2">
    <name2 name="ChildName1|ChildName2">
</names>

And the corresponding CSV:

name1  ChildName1|ChildName2
name2  ChildName1|ChildName2

And the corresponding JSON:

{"names": [{"name1":"ChildName1|ChildName2"},{"name2":"ChildName1|ChildName2"}]}

XSLT Algorithm Conversion

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:random="http://exslt.org/random"
                extension-element-prefixes="random">

    <!-- Root template - Entry point for processing -->
    <xsl:template match="/">
        <!-- Define an array of items to shuffle -->
        <array>
            <item>A</item>
            <item>B</item>
            <item>C</item>
            <item>D</item>
            <item>E</item>
            <item>F</item>
            <item>G</item>
        </array>

        <!-- Shuffle the array and store the result -->
        <xsl:variable name="shuffled" select="shuffle(/array/item)" />

        <!-- Output the shuffled result -->
        <result>
            <xsl:for-each select="$shuffled">
                <shuffled-item>
                    <xsl:value-of select="." />
                </shuffled-item>
            </xsl:for-each>
        </result>
    </xsl:template>

    <!-- Fisher-Yates Shuffle Implementation -->
    <xsl:template name="shuffle">
        <xsl:param name="items" /> <!-- Input array of items to be shuffled -->

        <!-- Base case: If the array is empty, return an empty sequence -->
        <xsl:choose>
            <xsl:when test="not($items)">
                <xsl:variable name="result" select="()" /> <!-- Return an empty sequence -->
                <xsl:copy-of select="$result" />
            </xsl:when>

            <!-- Recursive case: Process one random item at a time -->
            <xsl:otherwise>
                <!-- Get the number of items in the array -->
                <xsl:variable name="length" select="count($items)" />

                <!-- Generate a random index (EXSLT random number scaled to the array length) -->
                <xsl:variable name="random-index"
                              select="round(random:random() * ($length - 1)) + 1" />

                <!-- Select the item at the random index -->
                <xsl:variable name="selected" select="$items[$random-index]" />

                <!-- Remove the selected item from the array -->
                <xsl:variable name="remaining" select="remove($items, $random-index)" />

                <!-- Recursively shuffle the remaining items and prepend the selected item -->
                <xsl:variable name="result"
                              select="($selected, shuffle($remaining))" />

                <!-- Output the shuffled sequence -->
                <xsl:copy-of select="$result" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
⚠️ **GitHub.com Fallback** ⚠️