Xml XSLT:将属性转换为子元素
我是XSLT新手,虽然将XML转换为HTML是相当可行的,但我正在努力将XML转换为XML。我想做的应该是相当简单的,但我已经无可救药地被卡住了。考虑这个XML文件:Xml XSLT:将属性转换为子元素,xml,xslt,Xml,Xslt,我是XSLT新手,虽然将XML转换为HTML是相当可行的,但我正在努力将XML转换为XML。我想做的应该是相当简单的,但我已经无可救药地被卡住了。考虑这个XML文件: <cookbook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://jg.dhlab.de/Teaching/recipe.xsd">
<cookbook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://jg.dhlab.de/Teaching/recipe.xsd">
<recipe name="Apple pie">
<ingredients name='Apples'/>
<ingredients name='Butter'/>
<ingredients name="Flour"/>
<ingredients name="Cinnamon"/>
<ingredients name="Sugar"/>
<ingredients name="Eggs"/>
<instructions>In a small bowl, combine the sugars, flour and spices; set aside. In a large bowl, toss apples with lemon juice. Add sugar mixture; toss to coat. Line a 9-in. pie plate with bottom crust; trim even with edge. Fill with apple mixture; dot with butter. Roll remaining crust to fit top of pie; place over filling. Trim, seal and flute edges. Cut slits in crust.
Beat egg white until foamy; brush over crust. Sprinkle with sugar. Cover edges loosely with foil. Bake at 375° for 25 minutes. Remove foil and bake until crust is golden brown and filling is bubbly, 20-25 minutes longer. Cool on a wire rack.</instructions>
</recipe>
</cookbook>
在一个小碗里,混合糖、面粉和香料;放在一边。在一个大碗里,把苹果和柠檬汁一起搅拌。加入糖混合物;掷硬币穿衣服。a行9英寸。底皮饼盘;修剪整齐,边缘整齐。填充苹果混合物;用黄油打点。把剩下的面包皮滚到馅饼的顶部;放在填充物上。修剪、密封和凹槽边缘。在面包皮上切几个缝。
蛋清打至起泡;刷过外壳。撒上糖。用箔纸松散地盖住边缘。在375°下烘烤25分钟。取出铝箔,烘烤至外壳呈金棕色,馅料呈气泡状,再烤20-25分钟。在金属架上冷却。
我想将配料元素的name属性转换为子元素(即保留配料元素的同时)。我使用的XSL:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ingredients">
<xsl:element name="{name(@name)}">
<xsl:value-of select="@name"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但是,此XSL将name属性转换为元素,但会覆盖Components元素
<recipe name="Apple pie">
<ingredients>
<name>Apples</name>
<name>Butter</name>
<name>Flour</name>
<name>Cinnamon</name>
<name>Sugar</name>
<name>Eggs</name>
</ingredients>
<instructions>In a small bowl, combine the sugars, flour and spices; set aside. In a large bowl, toss apples with lemon juice. Add sugar mixture; toss to coat. Line a 9-in. pie plate with bottom crust; trim even with edge. Fill with apple mixture; dot with butter. Roll remaining crust to fit top of pie; place over filling. Trim, seal and flute edges. Cut slits in crust.
Beat egg white until foamy; brush over crust. Sprinkle with sugar. Cover edges loosely with foil. Bake at 375° for 25 minutes. Remove foil and bake until crust is golden brown and filling is bubbly, 20-25 minutes longer. Cool on a wire rack.</instructions>
</recipe>
苹果
黄油
面粉
肉桂色
糖
鸡蛋
在一个小碗里,将糖、面粉和香料混合,放在一边。在一个大碗里,把苹果和柠檬汁一起搅拌。加入糖混合物,搅拌到外壳上。将一个9英寸的馅饼盘与底部的外壳排成一行;用边缘修剪均匀。用苹果混合物填充;用黄油打点。将剩余的外壳卷成适合馅饼顶部的形状;放在馅料上。修剪、密封并用笛子刮边缘。切缝在地壳中。
将蛋清打至起泡,刷在蛋壳上。撒上糖。用箔纸松散地盖住边缘。在375°下烘烤25分钟。取出箔纸,烘烤至蛋壳呈金黄色,馅料呈气泡状,再烘烤20-25分钟。在金属架上冷却。
有人能告诉我如何实现这一点以及我做错了什么吗?如果您知道要创建
name
元素,请在模板中使用
和match=“components”
要创建包装器元素,请使用
<xsl:template match="recipe">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:where-populated>
<ingredients>
<xsl:apply-templates select="ingredients"/>
</ingredients>
</xsl:where-populated>
<xsl:apply-templates select="* except ingredients"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ingredients">
<name>
<xsl:value-of select="@name"/>
</name>
</xsl:template>
如果知道要创建
name
元素,请在模板中使用
和match=“components”
要创建包装器元素,请使用
<xsl:template match="recipe">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:where-populated>
<ingredients>
<xsl:apply-templates select="ingredients"/>
</ingredients>
</xsl:where-populated>
<xsl:apply-templates select="* except ingredients"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ingredients">
<name>
<xsl:value-of select="@name"/>
</name>
</xsl:template>
我试过了,但不管出于什么原因,我得到了这样的结果:苹果黄油面粉肉桂糖鸡蛋放在一个小碗里,混合糖….
@Jaap,如果需要对name
元素进行转换,请参见编辑d唯一缺少的问题是成分
父包装,然后编辑应该修复它。我已经尝试过了,但无论出于什么原因,我得到了这样的结果:苹果黄油面粉肉桂糖鸡蛋在一个小碗中,混合糖….
@Jaap,请参阅编辑,如果对name
元素的转换是根据需要进行的,并且唯一缺少的问题是成分
父包装,那么编辑应该修复它。