【发布时间】:2018-12-07 13:57:28
【问题描述】:
我有以下输入 XML,我想添加一个父标记(项目)并将所有项目放入其中,我是 XSLT 的新手我尝试了以下转换但我没有得到所需的输出:
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 正在为每个项目生成父标签。有人可以帮我吗 在这里。
【问题讨论】: