Solved

Using variables within XSL Output

Posted on 2003-10-28
7
246 Views
Last Modified: 2010-04-17
Hi,

I would ask this directly in the XML section but the link doesnt appear to be working.

I have the following piece of XSL code which embeds an SVG image within my document:

<xsl:for-each select="Diagram">
      <embed height="163.8"  width="172.20">
      <xsl:attribute name="id">SVGDiagram</xsl:attribute>
      <xsl:attribute name="src"><xsl:value-of select="//XSLPath"/>media/svg/feet_XSL.svg</xsl:attribute>      </embed>
</xsl:for-each>

I need to change the name of the embed (SVGDiagram) to unique identifiers e.g. append a 1,2 etc. so the embed name would be SVGDiagram1, SVGDiagram2 etc.

I believe this may be possible through using the tag <xsl:variable within the attribute tag, or by using some script within CDATA sections. Can anyone shed any light?

Cheers

0
Comment
Question by:paulsidebottom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:wellilein
ID: 9633209
I think

<xsl:variable name="enumerate" select="0"></xsl:variable>
<xsl:for-each select="Diagram">
      <xsl:variable name="enumerate" select="$enumerate+1"></xsl:variable>
     <embed height="163.8"  width="172.20">
     <xsl:attribute name="id">SVGDiagram<xsl:copy-of select="$enumerate"></xsl:copy-of></xsl:attribute>
     </embed>
</xsl:for-each>

should work, but XML-Spy has trouble with redefining the variable enumerate. I already encountered the problem once and I think the solution was to take a different transformer.
0
 

Expert Comment

by:berni_brown
ID: 9633227
Hi,

How are you intending to access the XSL file, which XML processor are you using?

0
 
LVL 2

Expert Comment

by:wellilein
ID: 9633269
If the only purpose is to distinguish Names, you might try position()

<xsl:for-each select="Diagram">
<xsl:variable name="enumerate" select="position()"/>
     <embed height="163.8"  width="172.20">
     <xsl:attribute name="id">SVGDiagram<xsl:copy-of select="$enumerate"></xsl:copy-of></xsl:attribute>
     </embed>
</xsl:for-each>

This also works in XML Spy.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:paulsidebottom
ID: 9633452
Thanks but...my XSL translator (IE5.5) is having problems with the redefiniton of enumerate as well.

Problem is, due to the way the XML is structured (diagram elements are grouped under a parent ), all elements named diagram return a position of 1, hence I can't use <xsl:value-of select="position()">

Thanks for your input so far though!

0
 
LVL 1

Author Comment

by:paulsidebottom
ID: 9633624
Sorry forgot to mention I am using MSXML3.0 XML parser
0
 
LVL 2

Accepted Solution

by:
wellilein earned 225 total points
ID: 9633722
Ok, try using the position of another element, maybe the parent element.

      <xsl:for-each select="Parent">
            <xsl:variable name="enumerate" select="position()"></xsl:variable>
            <xsl:for-each select="Diagram">
                 <embed height="163.8"  width="172.20">
                 <xsl:attribute name="id">SVGDiagram<xsl:copy-of select="$enumerate"></xsl:copy-of></xsl:attribute>
                 </embed>
           </xsl:for-each>
      </xsl:for-each>

will work fine if Parent has a position and has only one diagram. (If the parent contains more than one diagram, you could perhaps define two variables and concatenate them.)
0
 
LVL 1

Author Comment

by:paulsidebottom
ID: 9634077
Pure Genius! worked perfectly. Thanks a lot
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question