0

(first off: I'm terribly sorry that you have to look at this document structure; it's hideous)

I have the following XML document:

<MENUS>
  <MENU id="192748" servedate="20120213" mealid="3" mealname="Lunch" menuname="Cafeteria" menuid="26" totalcount="200">Cafeteria</MENU>
  <RECIPES>
    <NUTRIENTS>Calories~Energy (kcal)~kcal|Protein~Protein~gm|Fat~Fat~gm|Carbs~Total Carbohydrates~gm|Cholestrol~Cholesterol~mg|Calcium~Calcium~mg|Sodium~Sodium~mg|Iron~Iron~mg|</NUTRIENTS>
    <RECIPE id="6461-200" plucode="" shortname="Chipotle Spinach" numservings="100" portion="4 ounces" isselected="0" ismainitem="0" group="On the Side" publishingdescription="Chipotle Spinach" publishingtext="" enticingdescription="" price="1.53" category="Vegetables" productionarea="Hot Production" nutrients="152|2.3|13.8|6.5|0|74|346|1.85|" nutrientsuncertain="0|0|0|0|0|0|0|0|">Chipotle Spinach,4U</RECIPE>
    <RECIPE id="6586-300" plucode="" shortname="Asiago Crusted Chix" numservings="120" portion="3-3/4 ounces" isselected="0" ismainitem="0" group="Main Fare" publishingdescription="Asiago Crusted Chicken" publishingtext="" enticingdescription="" price="2.25" category="Chicken" productionarea="Hot Production" nutrients="203|19.6|7.6|13.2|56|124|387|1.37|" nutrientsuncertain="0|0|0|0|0|0|0|0|">Asiago Crusted Chicken,4U</RECIPE>
    <!-- any number of <RECIPE> elements ... -->
  </RECIPES>
</MENUS>

The <NUTRIENTS> element contains a pipe-delimited string; the components of this string need to somehow become elements for each <RECIPE>. Furthermore, the values of these new elements are specified by looking at the corresponding position within the pipe-delimited string found in <RECIPE>\<nutrients>.

The overall structure I'm shooting for is:

  1. All of the attributes for a <RECIPE> element are converted into child elements.
  2. The elements of <RECIPE>/<nutrients> map to the same position within the <NUTRIENTS> element.
  3. Using XSLT 1.0.

So, here would be my expected structure:

<?xml version="1.0"?>
<MENUS>
  <MENU id="192748" servedate="20120213" mealid="3" mealname="Lunch" menuname="Cafeteria" menuid="26" totalcount="200">Cafeteria</MENU>
  <RECIPES>
    <NUTRIENTS>Calories~Energy (kcal)~kcal|Protein~Protein~gm|Fat~Fat~gm|Carbs~Total Carbohydrates~gm|Cholestrol~Cholesterol~mg|Calcium~Calcium~mg|Sodium~Sodium~mg|Iron~Iron~mg|</NUTRIENTS>
    <RECIPE>
      <id>6461-200</id>
      <plucode/>
      <shortname>Chipotle Spinach</shortname>
      <numservings>100</numservings>
      <portion>4 ounces</portion>
      <isselected>0</isselected>
      <ismainitem>0</ismainitem>
      <group>On the Side</group>
      <publishingdescription>Chipotle Spinach</publishingdescription>
      <publishingtext/>
      <enticingdescription/>
      <price>1.53</price>
      <category>Vegetables</category>
      <productionarea>Hot Production</productionarea>
      <nutrients>152|2.3|13.8|6.5|0|74|346|1.85|</nutrients>
      <nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
      <CaloriesEnergykcalkcal>152</CaloriesEnergykcalkcal>
      <ProteinProteingm>2.3</ProteinProteingm>
      <FatFatgm>13.8</FatFatgm>
      <CarbsTotalCarbohydrates>6.5</CarbsTotalCarbohydrates>
      <CholestrolCholestrolmg>0</CholestrolCholestrolmg>
      <CalciumCalciummg>74</CalciumCalciummg>
      <SodiumSodiummg>346</SodiumSodiummg>
      <IronIronmg>1.85</IronIronmg>
    </RECIPE>
    <RECIPE>
      <id>6586-300</id>
      <plucode/>
      <shortname>Asiago Crusted Chix</shortname>
      <numservings>120</numservings>
      <portion>3-3/4 ounces</portion>
      <isselected>0</isselected>
      <ismainitem>0</ismainitem>
      <group>Main Fare</group>
      <publishingdescription>Asiago Crusted Chicken</publishingdescription>
      <publishingtext/>
      <enticingdescription/>
      <price>2.25</price>
      <category>Chicken</category>
      <productionarea>Hot Production</productionarea>
      <nutrients>203|19.6|7.6|13.2|56|124|387|1.37|</nutrients>
      <nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
      <CaloriesEnergykcalkcal>203</CaloriesEnergykcalkcal>
      <ProteinProteingm>19.6</ProteinProteingm>
      <FatFatgm>7.6</FatFatgm>
      <CarbsTotalCarbohydrates>13.2</CarbsTotalCarbohydrates>
      <CholestrolCholestrolmg>56</CholestrolCholestrolmg>
      <CalciumCalciummg>124</CalciumCalciummg>
      <SodiumSodiummg>387</SodiumSodiummg>
      <IronIronmg>1.37</IronIronmg>
    </RECIPE>
    <!-- ... -->
  </RECIPES>
</MENUS>

(notice, again, that I don't care about the field names we use for these new data points [which begin after <nutrientsuncertain>]; however, bonus points if you would like to show me how to relatively easily specify some sort of array, for lack of a better term, of field names)

Here's my current XSLT, which achieves goal #1; it's goal #2 that I'm stumped on:

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

  <!-- Template #1 - Identity Transform -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- Template #2 - Convert all of a <RECIPE> element's attributes to child elements -->  
  <xsl:template match="RECIPE/@*">
    <xsl:element name="{name()}">
      <xsl:value-of select="." />
    </xsl:element>
  </xsl:template>

  <!-- Template #3 - Remove extraneous text from each <RECIPE> -->  
  <xsl:template match="RECIPE/text()" />

</xsl:stylesheet>

That's it. Thanks so much for your help!

ABach
  • 3,743
  • 5
  • 25
  • 33

3 Answers3

1
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    exclude-result-prefixes="exsl"
    version="1.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:strip-space elements="*"/>

    <xsl:template match="*|@*|text()">
        <xsl:copy>
            <xsl:apply-templates select="*|@*|text()"/>
        </xsl:copy>
    </xsl:template>

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

            <xsl:variable name="nutrients-table-tmp">
                <xsl:call-template name="tokenize-table">
                    <xsl:with-param name="text" select="../NUTRIENTS/text()"/>
                    <xsl:with-param name="delimiter-row" select="'|'"/>
                    <xsl:with-param name="delimiter-col" select="'~'"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="nutrients-table" select="exsl:node-set($nutrients-table-tmp)/table"/>

            <xsl:variable name="nutrients">
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="@nutrients"/>
                    <xsl:with-param name="delimiter" select="'|'"/>
                </xsl:call-template>
            </xsl:variable>

            <xsl:for-each select="exsl:node-set($nutrients)/token">
                <xsl:variable name="pos" select="position()"/>
                <xsl:variable name="value" select="text()"/>
                <xsl:variable name="row" select="$nutrients-table/row[$pos]"/>

                <xsl:variable name="name" select="$row/cell[1]/text()"/>
                <xsl:variable name="description" select="$row/cell[2]/text()"/>
                <xsl:variable name="unit" select="$row/cell[3]/text()"/>

                <xsl:element name="{$name}">
                    <xsl:attribute name="unit">
                        <xsl:value-of select="$unit"/>
                    </xsl:attribute>
                    <xsl:value-of select="$value"/>
                </xsl:element>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="RECIPE/@*">
        <xsl:element name="{name()}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

    <xsl:template name="tokenize">
        <xsl:param name="text"/>
        <xsl:param name="delimiter" select="' '"/>
        <xsl:choose>

            <xsl:when test="contains($text,$delimiter)">
                <token>
                    <xsl:value-of select="substring-before($text,$delimiter)"/>
                </token>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-after($text,$delimiter)"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$text">
                <token>
                    <xsl:value-of select="$text"/>
                </token>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="tokenize-table">
        <xsl:param name="text"/>
        <xsl:param name="delimiter-row"/>
        <xsl:param name="delimiter-col"/>
        <xsl:variable name="rows">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="$text"/>
                <xsl:with-param name="delimiter" select="$delimiter-row"/>
            </xsl:call-template>
        </xsl:variable>
        <table>
            <xsl:for-each select="exsl:node-set($rows)/token">
                <xsl:variable name="items">
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="text" select="text()"/>
                        <xsl:with-param name="delimiter" select="$delimiter-col"/>
                    </xsl:call-template>
                </xsl:variable>
                <row>
                    <xsl:for-each select="exsl:node-set($items)/token">
                        <cell>
                            <xsl:value-of select="text()"/>
                        </cell>
                    </xsl:for-each>
                </row>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="utf-8"?>
<MENUS>
   <MENU id="192748" servedate="20120213" mealid="3" mealname="Lunch" menuname="Cafeteria" menuid="26" totalcount="200">Cafeteria</MENU>
   <RECIPES>
      <NUTRIENTS>Calories~Energy (kcal)~kcal|Protein~Protein~gm|Fat~Fat~gm|Carbs~Total Carbohydrates~gm|Cholestrol~Cholesterol~mg|Calcium~Calcium~mg|Sodium~Sodium~mg|Iron~Iron~mg|</NUTRIENTS>
      <RECIPE>
         <id>6461-200</id>
         <plucode/>
         <shortname>Chipotle Spinach</shortname>
         <numservings>100</numservings>
         <portion>4 ounces</portion>
         <isselected>0</isselected>
         <ismainitem>0</ismainitem>
         <group>On the Side</group>
         <publishingdescription>Chipotle Spinach</publishingdescription>
         <publishingtext/>
         <enticingdescription/>
         <price>1.53</price>
         <category>Vegetables</category>
         <productionarea>Hot Production</productionarea>
         <nutrients>152|2.3|13.8|6.5|0|74|346|1.85|</nutrients>
         <nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
         <Calories unit="kcal">152</Calories>
         <Protein unit="gm">2.3</Protein>
         <Fat unit="gm">13.8</Fat>
         <Carbs unit="gm">6.5</Carbs>
         <Cholestrol unit="mg">0</Cholestrol>
         <Calcium unit="mg">74</Calcium>
         <Sodium unit="mg">346</Sodium>
         <Iron unit="mg">1.85</Iron>
      </RECIPE>
      <RECIPE>
         <id>6586-300</id>
         <plucode/>
         <shortname>Asiago Crusted Chix</shortname>
         <numservings>120</numservings>
         <portion>3-3/4 ounces</portion>
         <isselected>0</isselected>
         <ismainitem>0</ismainitem>
         <group>Main Fare</group>
         <publishingdescription>Asiago Crusted Chicken</publishingdescription>
         <publishingtext/>
         <enticingdescription/>
         <price>2.25</price>
         <category>Chicken</category>
         <productionarea>Hot Production</productionarea>
         <nutrients>203|19.6|7.6|13.2|56|124|387|1.37|</nutrients>
         <nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
         <Calories unit="kcal">203</Calories>
         <Protein unit="gm">19.6</Protein>
         <Fat unit="gm">7.6</Fat>
         <Carbs unit="gm">13.2</Carbs>
         <Cholestrol unit="mg">56</Cholestrol>
         <Calcium unit="mg">124</Calcium>
         <Sodium unit="mg">387</Sodium>
         <Iron unit="mg">1.37</Iron>
      </RECIPE>
   </RECIPES>
</MENUS>
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
0

I am not 100% sure I understand your question, but I think you should take a look at the XSL tokenize function. If you combine this in a variable with the position() function you should be able to achieve that correlated output?

To further add to this, you can combine the name() with a (replace for 2.0/translate for 1.0) to get the element name automatically) within a for-each, extracting the positions.

AlwaysWrong
  • 526
  • 4
  • 9
  • Let me make one clarification (which I will update the original question with): I'm using 1.0. Am I correct in thinking that tokenize() is a 2.0 function? – ABach Feb 14 '12 at 18:39
  • Looks like some people have addressed that issue: http://stackoverflow.com/questions/1018974/tokenizing-and-sorting-with-xslt-1-0 – AlwaysWrong Feb 14 '12 at 18:55
-1

Refer my implementation:-

XSLT File:

<?xml version="1.0" encoding="UTF-8"?>
<!--<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"/>
</xsl:stylesheet>-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="no" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- Template #1 - Identity Transform -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <!-- Template #2 - Convert all of a <RECIPE> element's attributes to child elements -->
    <xsl:template match="RECIPE/@*">
        <xsl:element name="{name()}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

    <!-- Template #3 - Remove extraneous text from each <RECIPE> -->
    <xsl:template match="RECIPE/text()"/>

    <!-- Identifying the last attribute -->
    <xsl:template match="RECIPE/@*[position()=last()]">
        <xsl:element name="{name()}">
            <xsl:value-of select="."/>
        </xsl:element>

        <!--- Call the String Tokenize template -->
        <xsl:call-template name="tokenize">
            <xsl:with-param name="string" select="/MENUS/RECIPES/NUTRIENTS/text()"/>
            <xsl:with-param name="strValue" select="/MENUS/RECIPES/RECIPE/@nutrients"/>
        </xsl:call-template>
    </xsl:template>

    <!--- String Tokenize -->
    <xsl:template name="tokenize">
        <xsl:param name="string"/>
        <xsl:param name="strValue"/>
        <xsl:param name="delimiter" select="'|'"/>
        <xsl:choose>
            <xsl:when test="$delimiter and contains($string, $delimiter) and contains($strValue, $delimiter)">
                <xsl:variable name="subbef" select="translate(substring-before($string, $delimiter), '&#40;&#41;&#126;&#32;', '')"/>
                <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                <xsl:value-of select="$subbef"/>
                <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                <xsl:value-of select="substring-before($strValue, $delimiter)"/>
                <xsl:text disable-output-escaping="yes">&lt;/</xsl:text>
                <xsl:value-of select="$subbef"/>
                <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="string" select="translate(substring-after($string, $delimiter), '&#40;&#41;&#126;&#32;', '')"/>
                    <xsl:with-param name="strValue" select="substring-after($strValue, $delimiter)"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="string($string) and string($strValue)">
                    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                    <xsl:value-of select="$string"/>
                    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                    <xsl:value-of select="$strValue"/>
                    <xsl:text disable-output-escaping="yes">&lt;/</xsl:text>
                    <xsl:value-of select="$string"/>
                    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

OUTPUT:

<?xml version="1.0" encoding="UTF-8"?>
<MENUS>
    <MENU id="192748" servedate="20120213" mealid="3" mealname="Lunch" menuname="Cafeteria" menuid="26" totalcount="200">Cafeteria</MENU>
    <RECIPES>
        <NUTRIENTS>Calories~Energy (kcal)~kcal|Protein~Protein~gm|Fat~Fat~gm|Carbs~Total Carbohydrates~gm|Cholestrol~Cholesterol~mg|Calcium~Calcium~mg|Sodium~Sodium~mg|Iron~Iron~mg|</NUTRIENTS>
        <RECIPE>
            <id>6461-200</id>
            <plucode />
            <shortname>Chipotle Spinach</shortname>
            <numservings>100</numservings>
            <portion>4 ounces</portion>
            <isselected>0</isselected>
            <ismainitem>0</ismainitem>
            <group>On the Side</group>
            <publishingdescription>Chipotle Spinach</publishingdescription>
            <publishingtext />
            <enticingdescription />
            <price>1.53</price>
            <category>Vegetables</category>
            <productionarea>Hot Production</productionarea>
            <nutrients>152|2.3|13.8|6.5|0|74|346|1.85|</nutrients>
            <nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
            <CaloriesEnergykcalkcal>152</CaloriesEnergykcalkcal>
            <ProteinProteingm>2.3</ProteinProteingm>
            <FatFatgm>13.8</FatFatgm>
            <CarbsTotalCarbohydratesgm>6.5</CarbsTotalCarbohydratesgm>
            <CholestrolCholesterolmg>0</CholestrolCholesterolmg>
            <CalciumCalciummg>74</CalciumCalciummg>
            <SodiumSodiummg>346</SodiumSodiummg>
            <IronIronmg>1.85</IronIronmg>
        </RECIPE>
        <RECIPE>
            <id>6586-300</id>
            <plucode />
            <shortname>Asiago Crusted Chix</shortname>
            <numservings>120</numservings>
            <portion>3-3/4 ounces</portion>
            <isselected>0</isselected>
            <ismainitem>0</ismainitem>
            <group>Main Fare</group>
            <publishingdescription>Asiago Crusted Chicken</publishingdescription>
            <publishingtext />
            <enticingdescription />
            <price>2.25</price>
            <category>Chicken</category>
            <productionarea>Hot Production</productionarea>
            <nutrients>203|19.6|7.6|13.2|56|124|387|1.37|</nutrients>
            <nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
            <CaloriesEnergykcalkcal>152</CaloriesEnergykcalkcal>
            <ProteinProteingm>2.3</ProteinProteingm>
            <FatFatgm>13.8</FatFatgm>
            <CarbsTotalCarbohydratesgm>6.5</CarbsTotalCarbohydratesgm>
            <CholestrolCholesterolmg>0</CholestrolCholesterolmg>
            <CalciumCalciummg>74</CalciumCalciummg>
            <SodiumSodiummg>346</SodiumSodiummg>
            <IronIronmg>1.85</IronIronmg>
        </RECIPE>
        <!-- any number of <RECIPE> elements ... -->
    </RECIPES>
</MENUS>
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • I ran this through Saxon 9.4.0.2 and got the same results as my original XSLT (i.e., no new elements were added). – ABach Feb 14 '12 at 20:40
  • @ABach: I have tested in XMLSPY before i post it and its working as expected. I think, you might have missed something. – Siva Charan Feb 15 '12 at 03:51