我有以下输入XML,我想添加一个父标记(项目)并将所有项目放入其中,我是XSLT的新手,我尝试了下面的Transformation,但没有得到所需的输出:
Input.xml
<rootnode>
<header>
<head1>Food</head1>
<head2>Vegetables</head2>
</header>
<item>
<i1>Tomato</i1>
<i2>100</i2>
</item>
<item>
<i1>Brinjal</i1>
<i2>50</i2>
</item>
<item>
<i1>carrots</i1>
<i2>10</i2>
</item>
</rootnode>
我的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/rootnode">
<rootnode>
<xsl:apply-templates />
</rootnode>
</xsl:template>
<xsl:template match="/rootnode/header">
<xsl:copy-of select="."/>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="rootnode/item">
<items>
<xsl:copy-of select="."/>
<xsl:apply-templates />
</items>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
输出xml:
<?xml version="1.0" encoding="UTF-8"?>
<rootnode xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<header>
<head1>Food</head1>
<head2>Vegetables</head2>
</header>
<items>
<item>
<i1>Tomato</i1>
<i2>100</i2>
</item>
</items>
<items>
<item>
<i1>Brinjal</i1>
<i2>50</i2>
</item>
</items>
<items>
<item>
<i1>carrots</i1>
<i2>10</i2>
</item>
</items>
</rootnode>
必需的输出:
<rootnode>
<header>
<head1>Food</head1>
<head2>Vegetables</head2>
</header>
<items>
<item>
<i1>Tomato</i1>
<i2>100</i2>
</item>
<item>
<i1>Brinjal</i1>
<i2>50</i2>
</item>
<item>
<i1>carrots</i1>
<i2>10</i2>
</item>
</items>
</rootnode>
我想将所有项目的父标签(项目)作为集合,但是我的 xslt正在为每个项目生成父标签。有人可以帮帮我吗 在这里。
答案 0 :(得分:1)
为什么不简单:
<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:template match="/rootnode">
<xsl:copy>
<xsl:copy-of select="header"/>
<items>
<xsl:copy-of select="item"/>
</items>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>