XSLT 中的身份转换 [英] Identity transformation in XSLT
问题描述
我有这个源 XML:
<?xml version="1.0"?>
<root>
<item1>
<name>test</name>
<price>160</price>
<stock>4</stock>
<country>Belgium</country>
</item1>
<item2>
<name>Alfa</name>
<price>140</price>
<stock>3</stock>
<country>Turkey</country>
</item2>
<item3>
<name>Beta</name>
<price>110</price>
<stock>48</stock>
<country>Holland</country>
</item3>
<product id="p1">
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</product>
<product id="p2">
<name>Golf</name>
<price>1000</price>
<stock>5</stock>
<country>Germany</country>
</product>
<product id="p3">
<name>Alfa</name>
<price>1200</price>
<stock>19</stock>
<country>Germany</country>
</product>
<product id="p4">
<name>Foxtrot</name>
<price>1500</price>
<stock>5</stock>
<country>Australia</country>
</product>
<product id="p5">
<name>Tango</name>
<price>1225</price>
<stock>3</stock>
<country>Japan</country>
</product>
</root>
我希望它看起来像这样:
<?xml version="1.0"?>
<root>
<action>
<name>test</name>
<price>160</price>
<stock>4</stock>
<country>Belgium</country>
</action>
<action>
<name>Alfa</name>
<price>140</price>
<stock>3</stock>
<country>Turkey</country>
</action>
<action>
<name>Beta</name>
<price>110</price>
<stock>48</stock>
<country>Holland</country>
</action>
<action>
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</action>
<action>
<name>Golf</name>
<price>1000</price>
<stock>5</stock>
<country>Germany</country>
</action>
<action>
<name>Alfa</name>
<price>1200</price>
<stock>19</stock>
<country>Germany</country>
</action>
<action>
<name>Foxtrot</name>
<price>1500</price>
<stock>5</stock>
<country>Australia</country>
</action>
<action>
<name>Tango</name>
<price>1225</price>
<stock>3</stock>
<country>Japan</country>
</action>
</root>
所以
到
<action>
而
失去了属性,也变成了
And the <product>
loses its attribute, and also turns into
<action>
有人可以告诉我如何用 XSLT 做到这一点吗?因为我现在有 2 个不同的 XSL.我想将它们组合起来以创建一个 XSL.
Can somebody show me how to do this with XSLT? Because I have 2 different XSL's now. And I want to combine them to create just one XSL.
提前致谢
推荐答案
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="product|*[starts-with(name(), 'item')]">
<action>
<xsl:apply-templates select="node()|@*"/>
</action>
</xsl:template>
<xsl:template match="product/@id"/>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<root>
<item1>
<name>test</name>
<price>160</price>
<stock>4</stock>
<country>Belgium</country>
</item1>
<item2>
<name>Alfa</name>
<price>140</price>
<stock>3</stock>
<country>Turkey</country>
</item2>
<item3>
<name>Beta</name>
<price>110</price>
<stock>48</stock>
<country>Holland</country>
</item3>
<product id="p1">
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</product>
<product id="p2">
<name>Golf</name>
<price>1000</price>
<stock>5</stock>
<country>Germany</country>
</product>
<product id="p3">
<name>Alfa</name>
<price>1200</price>
<stock>19</stock>
<country>Germany</country>
</product>
<product id="p4">
<name>Foxtrot</name>
<price>1500</price>
<stock>5</stock>
<country>Australia</country>
</product>
<product id="p5">
<name>Tango</name>
<price>1225</price>
<stock>3</stock>
<country>Japan</country>
</product>
</root>
产生想要的、正确的结果:
<root>
<action>
<name>test</name>
<price>160</price>
<stock>4</stock>
<country>Belgium</country>
</action>
<action>
<name>Alfa</name>
<price>140</price>
<stock>3</stock>
<country>Turkey</country>
</action>
<action>
<name>Beta</name>
<price>110</price>
<stock>48</stock>
<country>Holland</country>
</action>
<action>
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</action>
<action>
<name>Golf</name>
<price>1000</price>
<stock>5</stock>
<country>Germany</country>
</action>
<action>
<name>Alfa</name>
<price>1200</price>
<stock>19</stock>
<country>Germany</country>
</action>
<action>
<name>Foxtrot</name>
<price>1500</price>
<stock>5</stock>
<country>Australia</country>
</action>
<action>
<name>Tango</name>
<price>1225</price>
<stock>3</stock>
<country>Japan</country>
</action>
</root>
说明:
身份规则按原样"复制每个节点.
The identity rule copies every node "as-is".
我们有两个模板来覆盖身份规则.第一个模板匹配任何 product
或名称以字符串 item
开头的任何元素.它通过创建和输出 action
元素有效地将匹配的元素重命名"为 action
.
We have two templates overriding the identity rule. The first template matches any product
or any element whose name starts with the string item
. It effectively "renames" the matched element to action
by creating and outputting an action
element.
第二个覆盖模板匹配任何 product
元素的任何 id
属性.该模板没有正文,可以有效地删除"匹配的属性——它不会在输出中复制/重新创建.
The second overriding template matches any id
attribute of any product
element. This template has no body, which effectively "deletes" the matched attribute -- it is not copied/recreated in the output.
这篇关于XSLT 中的身份转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!