xsl unwanted next element - read attributes from tag and sub tag
I got really close this time ;)
Bellow there is :
- my xsl script
my xml file (that calls the script)
the output of the manipulation of the xsl on the xml (as shuold be)
My problem is that while the href and src value should be the same there is always one picture diff between them(pic3 and pic4) as though I'm calling the next pic element... yet I dont understand where I do that?
<xsl:for-each select="data/pics/pic">
<div>
开发者_运维百科 <a>
<xsl:attribute name="href">
<xsl:value-of select="@href" />
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="@src" />
</xsl:attribute>
</img>
</a>
</div>
</xsl:for-each>
And this is my xml
<data>
<pics>
<pic href="pics\pic05.jpg" src="pics\pic05.jpg"></pic>
<pic href="pics\pic04.jpg" src="pics\pic04.jpg"></pic>
<pic href="pics\pic03.jpg" src="pics\pic03.jpg"></pic>
<pic href="pics\pic02.jpg" src="pics\pic02.jpg"></pic>
<pic href="pics\pic01.jpg" src="pics\pic01.jpg"></pic>
</pics>
</data>
this should be the output:
<div>
<a href="pics\pic01.jpg">
<img src="pics\pic01.jpg">
</a>
</div>
<div>
<a href="pics\pic02.jpg">
<img src="pics\pic02.jpg">
</a>
</div>
<div>
<a href="pics\pic03.jpg">
<img src="pics\pic03.jpg">
</a>
</div>
<div>
<a href="pics\pic04.jpg">
<img src="pics\pic04.jpg">
</a>
</div>
<div>
<a href="pics\pic05.jpg">
<img src="pics\pic05.jpg">
</a>
</div>
THe XSLT processor you are using may have a bug, or you haven't provided the real xslt code and xml document.
The provided XSLT code fragment when included in a complete stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<xsl:for-each select="data/pics/pic">
<div>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@href" />
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="@src" />
</xsl:attribute>
</img>
</a>
</div>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
and applied on the provided XML document:
<data>
<pics>
<pic href="pics\pic05.jpg" src="pics\pic05.jpg"></pic>
<pic href="pics\pic04.jpg" src="pics\pic04.jpg"></pic>
<pic href="pics\pic03.jpg" src="pics\pic03.jpg"></pic>
<pic href="pics\pic02.jpg" src="pics\pic02.jpg"></pic>
<pic href="pics\pic01.jpg" src="pics\pic01.jpg"></pic>
</pics>
</data>
produces the wanted, correct result:
<html>
<div>
<a href="pics\pic05.jpg">
<img src="pics\pic05.jpg"/>
</a>
</div>
<div>
<a href="pics\pic04.jpg">
<img src="pics\pic04.jpg"/>
</a>
</div>
<div>
<a href="pics\pic03.jpg">
<img src="pics\pic03.jpg"/>
</a>
</div>
<div>
<a href="pics\pic02.jpg">
<img src="pics\pic02.jpg"/>
</a>
</div>
<div>
<a href="pics\pic01.jpg">
<img src="pics\pic01.jpg"/>
</a>
</div>
</html>
This transformation produced the same result when performed with several different XSLT processors, such as MSXML3,4,6, Saxon6.5 and AltovaXML (XML-SPY).
Do note, that the transformation can be refactored into a much shorter and readable one:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<xsl:for-each select="data/pics/pic">
<div>
<a href="{@href}">
<img src="{@src}"/>
</a>
</div>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
精彩评论