使用XSLT在XML中追加重复的元素名?
如果我有一个如下所示的XML文件:使用XSLT在XML中追加重复的元素名?,xml,xslt,Xml,Xslt,如果我有一个如下所示的XML文件: <properties> <property> <picture>http://example.com/image1.jpg</picture> <picture>http://example.com/image2.jpg</picture> <picture>http://example.com/image3.jpg&l
<properties>
<property>
<picture>http://example.com/image1.jpg</picture>
<picture>http://example.com/image2.jpg</picture>
<picture>http://example.com/image3.jpg</picture>
<picture>http://example.com/image4.jpg</picture>
<picture>http://example.com/image5.jpg</picture>
</property>
</properties>
<properties>
<property>
<picture1>http://example.com/image1.jpg</picture1>
<picture2>http://example.com/image2.jpg</picture2>
<picture3>http://example.com/image3.jpg</picture3>
<picture4>http://example.com/image4.jpg</picture4>
<picture5>http://example.com/image5.jpg</picture5>
</property>
</properties>
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
http://example.com/image5.jpg
如何将其转换为每个图片URL元素都是唯一的,如下所示:
<properties>
<property>
<picture>http://example.com/image1.jpg</picture>
<picture>http://example.com/image2.jpg</picture>
<picture>http://example.com/image3.jpg</picture>
<picture>http://example.com/image4.jpg</picture>
<picture>http://example.com/image5.jpg</picture>
</property>
</properties>
<properties>
<property>
<picture1>http://example.com/image1.jpg</picture1>
<picture2>http://example.com/image2.jpg</picture2>
<picture3>http://example.com/image3.jpg</picture3>
<picture4>http://example.com/image4.jpg</picture4>
<picture5>http://example.com/image5.jpg</picture5>
</property>
</properties>
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
http://example.com/image5.jpg
假设每个元素必须有相同数量的元素,即使某些元素包含空值(图片URL的数量因属性而异),是否正确?使用count(前面的同级::*)+1
获取当前元素的索引
完整示例:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- identity transform -->
<xsl:template match="node()|@*">
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>
<!-- override for picture elements to rename element -->
<xsl:template match="picture">
<xsl:element name="{name()}{count(preceding-sibling::*)+1}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
使用计数(前面的同级::*)+1
获取当前元素的索引
完整示例:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- identity transform -->
<xsl:template match="node()|@*">
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>
<!-- override for picture elements to rename element -->
<xsl:template match="picture">
<xsl:element name="{name()}{count(preceding-sibling::*)+1}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
此简短而简单的转换(不使用轴):
应用于提供的XML文档时:
<properties>
<property>
<picture>http://example.com/image1.jpg</picture>
<picture>http://example.com/image2.jpg</picture>
<picture>http://example.com/image3.jpg</picture>
<picture>http://example.com/image4.jpg</picture>
<picture>http://example.com/image5.jpg</picture>
</property>
</properties>
<properties>
<property>
<picture1>http://example.com/image1.jpg</picture1>
<picture2>http://example.com/image2.jpg</picture2>
<picture3>http://example.com/image3.jpg</picture3>
<picture4>http://example.com/image4.jpg</picture4>
<picture5>http://example.com/image5.jpg</picture5>
</property>
</properties>
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
http://example.com/image5.jpg
生成所需的正确结果:
<properties>
<property>
<picture>http://example.com/image1.jpg</picture>
<picture>http://example.com/image2.jpg</picture>
<picture>http://example.com/image3.jpg</picture>
<picture>http://example.com/image4.jpg</picture>
<picture>http://example.com/image5.jpg</picture>
</property>
</properties>
<properties>
<property>
<picture1>http://example.com/image1.jpg</picture1>
<picture2>http://example.com/image2.jpg</picture2>
<picture3>http://example.com/image3.jpg</picture3>
<picture4>http://example.com/image4.jpg</picture4>
<picture5>http://example.com/image5.jpg</picture5>
</property>
</properties>
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
http://example.com/image5.jpg
说明:
图片元素的
名称
属性中的函数此简短而简单的转换(未使用轴):
应用于提供的XML文档时:
<properties>
<property>
<picture>http://example.com/image1.jpg</picture>
<picture>http://example.com/image2.jpg</picture>
<picture>http://example.com/image3.jpg</picture>
<picture>http://example.com/image4.jpg</picture>
<picture>http://example.com/image5.jpg</picture>
</property>
</properties>
<properties>
<property>
<picture1>http://example.com/image1.jpg</picture1>
<picture2>http://example.com/image2.jpg</picture2>
<picture3>http://example.com/image3.jpg</picture3>
<picture4>http://example.com/image4.jpg</picture4>
<picture5>http://example.com/image5.jpg</picture5>
</property>
</properties>
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
http://example.com/image5.jpg
生成所需的正确结果:
<properties>
<property>
<picture>http://example.com/image1.jpg</picture>
<picture>http://example.com/image2.jpg</picture>
<picture>http://example.com/image3.jpg</picture>
<picture>http://example.com/image4.jpg</picture>
<picture>http://example.com/image5.jpg</picture>
</property>
</properties>
<properties>
<property>
<picture1>http://example.com/image1.jpg</picture1>
<picture2>http://example.com/image2.jpg</picture2>
<picture3>http://example.com/image3.jpg</picture3>
<picture4>http://example.com/image4.jpg</picture4>
<picture5>http://example.com/image5.jpg</picture5>
</property>
</properties>
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
http://example.com/image5.jpg
说明:
图片元素的
名称
属性中的函数你似乎遗漏了一个词:“每个uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。对吧,弗朗西斯?不,你不能。它还将计算空白节点,而不仅仅是元素。@FrancisAvila:实际上可以在AVT中使用
position()
——请参见我的答案。@Kurt,您也可以使用position()
而不是count(前面的同级::*)+1
。对吧,弗朗西斯?不,你不能。它还将计算空白节点,而不仅仅是元素。@FrancisAvila:实际上可以在AVT中使用position()。