【问题标题】:xsl unwanted next element - read attributes from tag and sub tagxsl 不需要的下一个元素 - 从标签和子标签中读取属性
【发布时间】:2011-04-15 06:36:19
【问题描述】:

这次我真的很接近了;)

下面有:

  1. 我的 xsl 脚本
  2. 我的 xml 文件(调用脚本)

  3. 在 xml 上操作 xsl 的输出(应该如此)

我的问题是,虽然 href 和 src 值应该相同,但总有一个 它们之间的图片差异(pic3 和 pic4)好像我正在调用下一个图片元素......但我不明白我在哪里做的?

 <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>

这是我的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> 

这应该是输出:

<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>

【问题讨论】:

  • 为我工作。我已将您的 for-each 包含在
  • 同上 rincewind 所说的。还要发布您从 XSL 样式表中获得的实际 XML 输出。
  • 查看我的回答,报告提供的转换实际上产生了想要的结果。
  • +1 @ rincewind :首先我很高兴我有这个错误,因为我了解到我可以使用 xsltproc 检查 xslt 结果......或者在我的情况下,我发现 eclipea 很容易使用:vogella.de/articles/XSLT/article.html .... 不管怎样,bug 消失了...一定是缓存问题。非常感谢 Asaf

标签: xml xslt


【解决方案1】:

您使用的 XSLT 处理器可能存在错误,或者您没有提供真正的 xslt 代码和 xml 文档。

包含在完整样式表中时提供的 XSLT 代码片段:

<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>

并应用于提供的 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>

产生想要的正确结果:

<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>

当使用几个不同的 XSLT 处理器(例如 MSXML3、4、6、Saxon6.5 和 AltovaXML (XML-SPY))执行此转换时,会产生相同的结果。

请注意,可以将转换重构为更短且可读性更强的转换:

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    相关资源
    最近更新 更多