2011-06-22 11 views
1

在XSL中构建嵌套属性的最佳方式是什么?在XSLT中使用多个嵌套属性

我的问题是,onmouseover是一个属性,img的src是属性。由制造商给出的当前错误是:

An item of type 'Element' cannot be constructed within a node of type 'Attribute'.

我曾经有多个属性这将是我的首选路线的问题,而是抛出一个错误:

Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added.

因为我已经尝试以下作为解决方法,但没有运气

<xsl:template name="Item3"> 
<xsl:param name="ItemID" /> 

<xsl:variable name="IMGSRC"> 
    <xsl:choose> 
    <xsl:when test="$ItemID = 'ST-18/NM/NM/36'"> 
     <xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-36','.jpg')"/> 
    </xsl:when> 
    <xsl:when test="$ItemID = 'ST-18/NM/NM/48'"> 
     <xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-48','.jpg')"/> 
    </xsl:when> 
    <xsl:when test="$ItemID = 'ST-18/NM/NM/72'"> 
     <xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-72','.jpg')"/> 
    </xsl:when> 
    <xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/12'"> 
     <xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-12','.jpg')"/> 
    </xsl:when> 
    <xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/24'"> 
     <xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-24','.jpg')"/> 
    </xsl:when> 
    <xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/36'"> 
     <xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-36','.jpg')"/> 
    </xsl:when> 
    <xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/48'"> 
     <xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-48','.jpg')"/> 
    </xsl:when> 
    <xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/60'"> 
     <xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-60','.jpg')"/> 
    </xsl:when> 
    <xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/72'"> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="concat('imagesCategories/',$ItemID,'.jpg')"/> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:variable> 
<xsl:choose> 
    <xsl:when test="Items/Item[@ItemID=$ItemID]"> 
    <xsl:attribute name="onmouseover"> 
     <xsl:text>ddrivetip('</xsl:text> 
     <img src="{$IMGSRC}"/> 

     <br /> 
     <b> 
     <xsl:value-of select="Items/Item[@ItemID=$ItemID]/@ItemID" /> 
     </b> 
     <br /> 
     <b> 
     <xsl:value-of select="Items/Item[@ItemID=$ItemID]/@ItemDescription" /> 
     </b> 
     <br /> 
     <br /> 
     <xsl:text>Price (01-09): </xsl:text> 
     <xsl:value-of select="Items/Item[@ItemID=$ItemID]/@PriceLevel1" /> 
     <br/> 
     <xsl:text>Price (10-24): </xsl:text> 
     <xsl:value-of select="Items/Item[@ItemID=$ItemID]/@PriceLevel2" /> 
     <br/> 
     <xsl:text>Price (25-49): </xsl:text> 
     <xsl:value-of select="Items/Item[@ItemID=$ItemID]/@PriceLevel3" /> 
     <br/> 
     <xsl:text>Qty In Stock: </xsl:text> 
     <xsl:value-of select="Items/Item[@ItemID=$ItemID]/@QtyOnHand" /> 
     <br /> 
     <br /> 
     <xsl:text>Click </xsl:text> 
     <b> 
     <xsl:text>"BUY!"</xsl:text> 
     </b> 
     <xsl:text> to add this item to your shopping cart</xsl:text> 
     <xsl:text>', '', '300')</xsl:text> 

    </xsl:attribute> 

有一些额外的代码,然后适当的结束标签。 谢谢大家!

+0

嵌套属性???它们不存在于HTML或XML中,为什么XSLT能够创建它们?您可能想向我们展示您想要创建的HTML或XML示例,也许您希望创建可行的内容,只有您的术语“嵌套属性”是错误的。 –

回答

1

它看起来像你试图将html作为字符串传递给ddrivetip函数。但是,您将这些节点添加为节点而不是文本,并且无法将节点添加到属性中,因此一种解决方案是将节点设置为文本(您必须避免括号和双引号)。

但是,您正在将大量信息放入onmouseover事件中,这是不推荐的。而不是你现在正在做什么,我会做一个隐藏的元素与一个id,它将你的itemId与你的html的内容结合起来,然后在你的onmouseover事件中根据需要显示它。

0

使用CDATA部分,以便XSLT处理器解释你的img标签作为文本节点的一部分,而不是试图插入元素节点插入一个属性(它是由XML规范禁止)

<xsl:attribute name="onmouseover"> 
    <xsl:text><![CDATA[ddrivetip('<img src="]]></xsl:text> 
    <xsl:value-of select="$IMGSRC" /> 
    <xsl:text><![CDATA["/> 

     <br /> 

    ...