>我有以下输入:
<Article>
<title>Apple</title>
<number>119.057</number>
<price>90.8</price>
<option>
<Article>
<title>Apple Green</title>
<price>144.2</price>
<number>119.086</number>
</Article>
</option>
</Article>
<Article>
<title>Coconut</title>
<number>120.882</number>
<price>10.00</price>
</Article>
<Article>
<title>Pinapple</title>
<number>120.883</number>
<price>19.00</price>
</Article>
它应该看起来像这样:
<Article>
<title>Apple</title>
<number>119.057</number>
<price>90.8</price>
</Article>
<Article>
<title>Apple Green</title>
<price>144.2</price>
<number>119.086</number>
</Article>
<Article>
<title>Coconut</title>
<number>120.882</number>
<price>10.00</price>
</Article>
<Article>
<title>Pinapple</title>
<number>120.883</number>
<price>19.00</price>
</Article>
到目前为止我的尝试:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" />
<xsl:template match="root/Article">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Article/option">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<Article>
<xsl:value-of select="*"/>
</Article>
</xsl:copy>
</xsl:template>
<!--Identity template-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
实际上,我需要删除选项标签,子标签应该成为文章标签。以便所有节点在树中处于同一级别最后删除选项 标签.
感谢您的投入!
你写道:"我需要删除options
标签,他们的子标签应该成为(直接的(Article
标签"。
然后这样做,但要稍作更正:您要匹配的标签(并在输出中删除(是option
(不是options
(。
<xsl:template match="Article/option">
<xsl:apply-templates />
</xsl:template>
请注意与标识模板的相似性。主要区别在于没有xsl:copy
标签(它会复制原始option
标签(。
编辑
最初我只展示了模板。下面是完整的脚本:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" />
<xsl:template match="Article/option">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
它确实会生成所需的输出。
编辑 2
又一个解决方案。它只复制option
内容,但保留正确的XML格式,我添加了一个"信封"根(main
(标签。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" />
<xsl:strip-space elements="*"/>
<xsl:template match="/Article">
<!-- This matches only the root Article tag, not its grandson -->
<main> <!-- You need to have a main (root) tag -->
<!-- Output only the option content -->
<xsl:apply-templates select="option"/>
</main>
</xsl:template>
<xsl:template match="option">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
给定格式正确的输入,例如:
.XML
<Articles>
<Article>
<title>Apple</title>
<number>119.057</number>
<price>90.8</price>
<option>
<Article>
<title>Apple Green</title>
<price>144.2</price>
<number>119.086</number>
</Article>
</option>
</Article>
<Article>
<title>Coconut</title>
<number>120.882</number>
<price>10.00</price>
</Article>
<Article>
<title>Pinapple</title>
<number>120.883</number>
<price>19.00</price>
</Article>
</Articles>
以下样式表:
XSLT 1.0
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Article">
<xsl:copy>
<xsl:apply-templates select="*[not(self::option)]"/>
</xsl:copy>
<xsl:apply-templates select="option"/>
</xsl:template>
<xsl:template match="option">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
将option
节点从Article
的子节点转换为其同级节点,返回:
结果
<?xml version="1.0" encoding="UTF-8"?>
<Articles>
<Article>
<title>Apple</title>
<number>119.057</number>
<price>90.8</price>
</Article>
<Article>
<title>Apple Green</title>
<price>144.2</price>
<number>119.086</number>
</Article>
<Article>
<title>Coconut</title>
<number>120.882</number>
<price>10.00</price>
</Article>
<Article>
<title>Pinapple</title>
<number>120.883</number>
<price>19.00</price>
</Article>
</Articles>