使用XSLT基于管道分隔的属性生成节点

时间:2012-02-14 18:14:06

标签: xslt xslt-1.0

(首先关闭:我非常抱歉你必须看看这个文档结构;它很可怕)

我有以下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。
  4. 所以,这是我预期的结构:

    <?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 个答案:

答案 0 :(得分: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>

输出:

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

答案 1 :(得分:0)

我不是100%确定我理解你的问题,但我认为你应该看看XSL tokenize function。如果将变量与position()函数结合使用,您应该能够实现相关输出吗?

要进一步添加到此,您可以将名称()与(替换为2.0 / translate for 1.0)组合以在for-each中自动获取元素名称,从而提取位置。

答案 2 :(得分:-1)

参考我的实施: -

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>