开发者

Using Multiple Nested Attributes in XSLT

What is the best way to construct nested attributes in XSL?

My issue is that onmouseover is an attribute and the src of img is an attribute. The current error given by the builder is:

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

I used to have an issue of multiple attributes which would have been my preferred route but throws an error:

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

I have since attempted the following as a workaround but with no luck

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

There is some additional code and then the proper closing ta开发者_StackOverflow社区gs. Thanks everyone!


It looks like you are trying to pass html as a string to your ddrivetip function. However, you are adding these as nodes instead of text, and nodes cannot be added added to attributes, so one solution is to make the nodes text (You'll have to escape the brackets and double quotes too).

However, you are putting a lot of information into the onmouseover event, which is not recommended. Instead of what you are currently doing, I would make a hidden element with an id that incorporates your itemId with the contents of your html and then show that as needed in your onmouseover event.


Use CDATA sections so that the XSLT Processor interpret your img tags as a part of text nodes and not as an attempt to insert element nodes into an attribute (it is forbidden by the XML specification)

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

      <br />

  ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜