xml - 我想在不更改其属性的情况下将 xml 元素“产品”重命名为“项目”,但我不知道该怎么做
问题描述
输入 XML:
<ProductList>
<Product Action="Manage" ProductID="Item_1">
<PrimaryInformation Description="Item 1"
Status="3000" ShortDescription="Item 1" />
</Product>
<Product Action="Manage" ProductID="Item_2">
<PrimaryInformation Description="Item 2"
Status="3000" ShortDescription="Item 2" />
</Product>
<Product Action="Manage" ProductID="Item_3">
<PrimaryInformation Description="Item 3"
Status="3000" ShortDescription="Item 3" />
</Product>
</ProductList>
预期的 XML 输出:
<ProductList >
<Item Action="Manage" ProductID="Item_1" >
<PrimaryInformation Description="Item 1" Status="3000" ShortDescription="Item 1" />
</Item>
<Item Action="Manage" ProductID="Item_2" >
<PrimaryInformation Description="Item 2" Status="3000" ShortDescription="Item 2" />
</Item>
<Item Action="Manage" ProductID="Item_3" >
<PrimaryInformation Description="Item 3" Status="3000" ShortDescription="Item 3" />
</Item>
</ProductList>
我尝试了这个 XML 模板,它正在工作,但它也删除了该元素中存在的所有属性。
<xsl:template match="Product">
<xsl:element name="Item">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
解决方案
请尝试以下 XSLT。
它遵循身份转换模式。
XSLT
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Product">
<Item >
<xsl:apply-templates select="@*|node()"/>
</Item >
</xsl:template>
</xsl:stylesheet>
推荐阅读
- data-visualization - 如何在 Apache Superset 中编辑查询?
- puppet - 将符号链接的内容复制到目录
- algorithm - 找到比 O(n^2) 更好的相似名称
- git - 更改 git branch -vv 的格式
- javascript - 如何使用下拉菜单更改背景颜色渐变
- javascript - 如何独立调用 d3.svg.line()?
- mysql - 优化最新时间戳查询mysql
- android - Android使用重力=左调整ImageView的大小
- ffmpeg - 使用 Android NDK r17 构建 C 库 (FFmpeg):未定义对“__mulodi4”的引用
- ajax - 在 Angular 5 中动态地将 routerLink 添加到通过 AJAX 动态创建的组件中