我想在不更改其属性的情况下将 xml 元素 "product" 重命名为 "Item",但我不知道该怎么做
I want to rename xml element "product" to "Item" without changing its attributes,but i don't know how to do that
输入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>
输入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>