如何将属性及其值从一个元素移动到另一个元素
How to move an attribute and its value from one element to another
我有以下 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Orders>
<Order Weight="1.00">
<Items>
<Item ItemLength="5.00" ItemQty="1">
<ItemCharge Description="Chair" Amount="5.50"></ItemCharge>
</Item>
</Items>
</Order>
<Order Weight="2.50">
<Items>
<Item Length="5.00" ItemQty="1">
<ItemCharge Description="Chair" Amount="8.50"></ItemCharge>
</Item>
</Items>
</Order>
</Orders>
我需要将 "Order" 元素中的属性和值(例如 Weight="1.00")移动到 "Item" 元素中。 "Order" 元素的数量和 "Weight" 属性中的值会不时变化。期望的结果应如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Orders>
<Order>
<Items>
<Item ItemLength="5.00" ItemQty="1" Weight="1.00">
<ItemCharge Description="Chair" Amount="5.50"></ItemCharge>
</Item>
</Items>
</Order>
<Order>
<Items>
<Item Length="5.00" ItemQty="1" Weight="2.50">
<ItemCharge Description="Chair" Amount="8.50"></ItemCharge>
</Item>
</Items>
</Order>
</Orders>
我目前有这个 XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*[not(name() = 'Weight')] | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
非常感谢您的帮助!!!
您需要将此视为两个单独的更改,而不是一个 "move"
1) 删除 Order
元素的 Weight
属性。或者更确切地说,不要将 Weight
属性复制到输出
<xsl:template match="@Weight" />
2) 将 Item
元素复制到输出时添加一个 Weight
属性到输出
<xsl:template match="Item">
<Item Weight="{../../@Weight}">
<xsl:apply-templates select="@* | node()"/>
</Item>
</xsl:template>
注意在创建新属性时使用 "Attribute Value Templates"。
另请注意,您应避免在此类转换中更改标识模板。上面的两个模板,因为它们使用特定的名称,所以比身份模板获得更高的优先级。
试试这个 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@Weight" />
<xsl:template match="Item">
<Item Weight="{../../@Weight}">
<xsl:apply-templates select="@* | node()"/>
</Item>
</xsl:template>
</xsl:stylesheet>
我有以下 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Orders>
<Order Weight="1.00">
<Items>
<Item ItemLength="5.00" ItemQty="1">
<ItemCharge Description="Chair" Amount="5.50"></ItemCharge>
</Item>
</Items>
</Order>
<Order Weight="2.50">
<Items>
<Item Length="5.00" ItemQty="1">
<ItemCharge Description="Chair" Amount="8.50"></ItemCharge>
</Item>
</Items>
</Order>
</Orders>
我需要将 "Order" 元素中的属性和值(例如 Weight="1.00")移动到 "Item" 元素中。 "Order" 元素的数量和 "Weight" 属性中的值会不时变化。期望的结果应如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Orders>
<Order>
<Items>
<Item ItemLength="5.00" ItemQty="1" Weight="1.00">
<ItemCharge Description="Chair" Amount="5.50"></ItemCharge>
</Item>
</Items>
</Order>
<Order>
<Items>
<Item Length="5.00" ItemQty="1" Weight="2.50">
<ItemCharge Description="Chair" Amount="8.50"></ItemCharge>
</Item>
</Items>
</Order>
</Orders>
我目前有这个 XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*[not(name() = 'Weight')] | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
非常感谢您的帮助!!!
您需要将此视为两个单独的更改,而不是一个 "move"
1) 删除 Order
元素的 Weight
属性。或者更确切地说,不要将 Weight
属性复制到输出
<xsl:template match="@Weight" />
2) 将 Item
元素复制到输出时添加一个 Weight
属性到输出
<xsl:template match="Item">
<Item Weight="{../../@Weight}">
<xsl:apply-templates select="@* | node()"/>
</Item>
</xsl:template>
注意在创建新属性时使用 "Attribute Value Templates"。
另请注意,您应避免在此类转换中更改标识模板。上面的两个模板,因为它们使用特定的名称,所以比身份模板获得更高的优先级。
试试这个 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@Weight" />
<xsl:template match="Item">
<Item Weight="{../../@Weight}">
<xsl:apply-templates select="@* | node()"/>
</Item>
</xsl:template>
</xsl:stylesheet>