Solved

Using variables within XSL Output

Posted on 2003-10-28
7
242 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

832 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