我有这个源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>
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)
所以<item1>要<item3转变为
<action>
Run Code Online (Sandbox Code Playgroud)
而<product>失去了它的属性,也变成了
<action>
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何使用XSLT做到这一点?因为我现在有2种不同的XSL.我想将它们组合起来只创建一个XSL.
提前致谢
Dim*_*hev 15
这种转变:
<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>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的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>
Run Code Online (Sandbox Code Playgroud)
产生想要的,正确的结果:
<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>
Run Code Online (Sandbox Code Playgroud)
说明:
身份规则"按原样"复制每个节点.
我们有两个模板覆盖了身份规则.第一个模板匹配product名称以字符串开头的任何或任何元素item.它action通过创建和输出action元素有效地"重命名"匹配的元素.
第二个重写模板匹配id任何product元素的任何属性.此模板没有主体,它有效地"删除"匹配的属性 - 它不会在输出中复制/重新创建.
| 归档时间: |
|
| 查看次数: |
6468 次 |
| 最近记录: |