我如何使用xslt将给定的xml转换为以下内容

时间:2019-01-30 17:41:40

标签: xml xslt

如果我遇到以下问题

我需要使用XSLT转换XML以产生<parent.child>value</parent.child><parent.child.grandchild>value</parent.child.grandchild>(如果存在Granchild)。 另外,如果我不希望特定元素说价格,则不应该打印...请帮助我制作XSLT吗? 它应该是通用代码,以便对于任何XML都可以产生以下输出。   我们应该得到这样的东西。

<food.name>Strawberry Belgian Waffles</food.name>
    <food.description>
    Light Belgian waffles covered with strawberries and whipped cream
    </food.description>
    <food.calories>900</food.calories>`
and so on..
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
   Two of our famous Belgian Waffles with plenty of real maple syrup
   </description>
    <calories>650</calories>
</food>
<food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>
    Light Belgian waffles covered with strawberries and whipped cream
    </description>
    <calories>900</calories>
</food>
<food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>
    Belgian waffles covered with assorted fresh berries and whipped cream
    </description>
    <calories>900</calories>
</food>
<food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>
    Thick slices made from our homemade sourdough bread
    </description>
    <calories>600</calories>
</food>
<food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>
    Two eggs, bacon or sausage, toast, and our ever-popular hash browns
    </description>
    <calories>950</calories>
</food>
</breakfast_menu>

1 个答案:

答案 0 :(得分:0)

以下样式表:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="*">
    <xsl:element name="{ancestor::*[parent::*]/concat(name(), '.')}{name()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

应用于您的示例输入,将返回:

结果

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
      <food>
            <food.name>Belgian Waffles</food.name>
            <food.price>$5.95</food.price>
            <food.description>
       Two of our famous Belgian Waffles with plenty of real maple syrup
       </food.description>
            <food.calories>650</food.calories>
      </food>
      <food>
            <food.name>Strawberry Belgian Waffles</food.name>
            <food.price>$7.95</food.price>
            <food.description>
        Light Belgian waffles covered with strawberries and whipped cream
        </food.description>
            <food.calories>900</food.calories>
      </food>
      <food>
            <food.name>Berry-Berry Belgian Waffles</food.name>
            <food.price>$8.95</food.price>
            <food.description>
        Belgian waffles covered with assorted fresh berries and whipped cream
        </food.description>
            <food.calories>900</food.calories>
      </food>
      <food>
            <food.name>French Toast</food.name>
            <food.price>$4.50</food.price>
            <food.description>
        Thick slices made from our homemade sourdough bread
        </food.description>
            <food.calories>600</food.calories>
      </food>
      <food>
            <food.name>Homestyle Breakfast</food.name>
            <food.price>$6.95</food.price>
            <food.description>
        Two eggs, bacon or sausage, toast, and our ever-popular hash browns
        </food.description>
            <food.calories>950</food.calories>
      </food>
</breakfast_menu>

如果您不希望包含诸如price之类的元素,只需添加一个与之匹配的空模板即可。