Использование XSLT для генерации узлов на основе атрибутов, разделенных каналом - PullRequest
0 голосов
/ 14 февраля 2012

(во-первых: мне очень жаль, что вам нужно взглянуть на эту структуру документа; она отвратительна)

У меня есть следующий XML-документ:

<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>

<NUTRIENTS> элемент содержит строку с разделителями в виде строки;компоненты этой строки должны как-то становиться элементами для каждого <RECIPE>.Кроме того, значения этих новых элементов задаются путем просмотра соответствующей позиции в строке с разделителями в виде строки, найденной в <RECIPE>\<nutrients>.

Общая структура, для которой я снимаю:

  1. Все атрибуты для элемента <RECIPE> преобразуются в дочерние элементы.
  2. Элементы<RECIPE>/<nutrients> сопоставить с той же позицией в элементе <NUTRIENTS>.
  3. Использование XSLT 1.0.

Итак, вот моя ожидаемая структура:

<?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>

(обратите внимание, опять же, что меня не волнуют имена полей, которые мы используем для этих новых точек данных [которые начинаются после <nutrientsuncertain>]; однако, бонусные баллы, если вы хотите показать мне, как относительно легко указать некоторыевроде массива, из-за отсутствия лучшего термина, имен полей)

Вот мой текущий XSLT, который достигает цели # 1;это цель # 2, на которой я поставлен в тупик:

<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>

Вот и все.Большое спасибо за вашу помощь!

Ответы [ 3 ]

1 голос
/ 15 февраля 2012
<?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>

Выход:

<?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>
0 голосов
/ 15 февраля 2012

См. Мою реализацию: -

XSLT-файл:

<?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>

ВЫХОД:

<?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>
0 голосов
/ 14 февраля 2012

Я не уверен на 100%, что понимаю ваш вопрос, но я думаю, вам стоит взглянуть на функцию токенизации XSL .Если вы скомбинируете это в переменной с функцией position (), вы сможете получить этот коррелированный вывод?

Чтобы еще больше добавить к этому, вы можете объединить name () с (заменить на 2.0 / translate).для 1.0) для автоматического получения имени элемента) в for-each, извлекая позиции.

...