Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT XML-to-SVG Problem...Is This Task Do-able? 1

Status
Not open for further replies.

DavidC2

Programmer
Joined
Jul 20, 2004
Messages
5
Location
US
Hi All!

I have beeen researching my problem for quite some time and haven't read any situation where someone else is doing what I need to do...it makes me wonder if anybody has attempted this task.I am working at the request of a client who has (paper-version) troubleshooting flowcharts, whereby logic trees are built on a structure given numerous Tests (rectangle), Malfunctions (diamond) and two answer-values Yes/No (circles) for each Test. The client would like for these flowcharts to be transformed from the XML files into SVG files that on being produced (I am supposing from a Perl/Java batch script), the YES/NO circles become clickable URI's which take the user to the next SVG file based on their selected decision.

I was successful at creating an XSLT which successfully performed the transformation for a 9-page (paper-version) flowchart; however, the client would like to have a one-XSLT-fits-all XML files...transforming each into a long flowing generated SVG which will navigate from every YES/NO to the appropriate new Test.

With the research that I have thus accomplished, I believe this to be an unrealistic task, primarily because each and every flowchart is unique...there are no two alike in any way in their tree structure. If they were all identical, then a template XSLT could be created, I think.

This brings me to my query. I have theorized to my colleagues that if were to break all of these flowcharts into individual elements and make them identical in form, where each work package has 1 Test, 1 Malfunction and 2 Decisions YES or NO, then it might then become possible to accomplish this task.

By breaking my XML source files into smaller packets and experimenting with flowing data into an SVG transformation, I was successful to that end; however, the largest issue that I just can't seem to crack is my XREF for the YES/NO value-answers. I am attempting to surround my YES and NO "<xsl:value-of select="@name"/>" with an <a> and in which the HREF value attribute is streamed from within the XREF element in order to create the appropriate URI for each YES/NO within the SVG's that are created.

Can anybody help? Is this task do-able or am I way off base?

My source files are as follows (for that particular snippet):

<!-- SOURCE: XSLT -->

<xsl:template match="select-value">
<xsl:for-each select="value-answer">
<xsl:if test="position() != last() and (@name='YES' or 'NO')">
<svg xml:space="preserve" width="100%" height="100%">
<text x="218" y="384" style="text-anchor: start; font-size: 8pt;">
<a
xmlns:xlink=" xlink:href="<!-- PROBLEM: NEED VALUE-OF "YES" XREF/HREF FROM SOURCE XML STREAMED HERE -->">
<xsl:value-of select="@name"/>
</a>
</text>
</svg>
</xsl:if>

<xsl:if test="position()=last() and (@name='YES' or 'NO')">
<svg xml:space="preserve" width="100%" height="100%">
<text x="79" y="464" style="text-anchor: start; font-size: 8pt;">
<a
xmlns:xlink=" xlink:href="<!-- PROBLEM: NEED VALUE-OF "NO" XREF/HREF FROM SOURCE XML STREAMED HERE -->">
<xsl:value-of select="@name"/>
</a>
</text>
</svg>
</xsl:if>
<xsl:apply-templates/>
</xsl:for-each>
</xsl:template>

<!-- SOURCE: XML -->

<select-value id="T00002-TS-11-5820-890-30-105" idtoken="idtokenT00002-TS-11-5820-890-30-105" name=" ">
<indication name="content">
<pcnarr>5 DIGIT PART OF DISPLAY LIT</pcnarr>
</indication>
<value-answer name="YES">
<testblockref>
<xref href="T00003.svg"/>
</testblockref>
</value-answer>
<value-answer name="NO">
<testblockref>
<xref href="T00002.svg"/>
</testblockref>
</value-answer>
</select-value>
 
Not sure if this is what you are looking for:

xlink:href="<!-- PROBLEM: NEED VALUE-OF "YES" XREF/HREF FROM SOURCE XML STREAMED HERE -->"

xlink:href="{@name}"

When you want to output the result of an XPath within an attribute of html or xml that is not correlated to an xsl: element...you can use the {} to enclose the XPath.

Also, just as a side note. If these files are created on the fly, the bigger you make your XSLT, the slower your translation will run and the more memory it will take to translate each time. If this translation only happens once in awhile, than its probably no big deal. But if it happens often, and you are programming in Java or something like that, you make look into APIs that allow you to precompile the XSLT files. This makes them much faster and removes many of the XSLT document memory constraints.

Just a side note.

Hope that helps,

-jay
 
Thanks, Jay.

That definitely got me going in the right direction. It pulled the YES @name as the HREF, but what I need to do is actually pull the HREF from within the XREF. Note below that the "value-answer" is nested in "select-value" (which is the actual <xsl:template>) and what I want is to pull the value of the HREF from the xref which is nested in the "value-answer/testblockref". In my xslt, I surrounded my "value-answer" @name with <a> in order to have that source "YES" or "NO" be the viewable link. But what I actually want to occur is to have the link be the value of the svg file that is contained in the HREF attribute.

Using your suggestion created the link "YES", so I know that works, but when I attempt to change your suggestion to xlink:href="{@href}", the transformed svg with text "YES" acts as a link (hand icon), but no URI is present (bottom-left status bar where actual link appears) and clicking produces nothing.

I am thinking that perhaps I am not creating the proper hierarchal structure needed to parse that isntruction.

Thanks so much for you assistance.

<!-- SOURCE: XML -->
<select-value id="T00002-TS-11-5820-890-30-105" idtoken="idtokenT00002-TS-11-5820-890-30-105" name=" ">
<indication name="content">
<pcnarr>5 DIGIT PART OF DISPLAY LIT</pcnarr>
</indication>
<value-answer name="YES">
<testblockref>
<xref href="T00003.svg"/>
</testblockref>
</value-answer>
<value-answer name="NO">
<testblockref>
<xref href="T00002.svg"/>
</testblockref>
</value-answer>
</select-value>
 
So you can do it this way

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:template match="select-value">        
   <xsl:for-each select="value-answer">
      <xsl:if test="position() != last() and (@name='YES' or 'NO')">
         <svg xml:space="preserve" width="100%" height="100%">
            <text x="218" y="384" style="text-anchor: start; font-size: 8pt;">
               <a
        xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL]
        xlink:href="{self::node()[@name='YES']/testblockref/xref/@href}">
        <xsl:value-of select="@name"/>
               </a>
            </text>
         </svg>
      </xsl:if>

      <xsl:if test="position()=last() and (@name='YES' or 'NO')">
         <svg xml:space="preserve" width="100%" height="100%">
            <text x="79" y="464" style="text-anchor: start; font-size: 8pt;">
               <a
        xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL]
        xlink:href="{self::node()[@name='NO']/testblockref/xref/@href}">
        <xsl:value-of select="@name"/>
               </a>
            </text>
         </svg>
      </xsl:if>
      <xsl:apply-templates/>
   </xsl:for-each>   
</xsl:template>
</xsl:stylesheet>

Or this way through templates

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
    <xsl:template match="select-value">    
        <xsl:if test="position() != last() and (@name='YES' or 'NO')">
            <xsl:apply-templates select="value-answer" mode="positionNotLast"/>
        </xsl:if>

        <xsl:if test="position()=last() and (@name='YES' or 'NO')">
            <xsl:apply-templates select="value-answer" mode="positionLast"/>
        </xsl:if>
    </xsl:template>
    <xsl:template match="value-answer" mode="positionNotLast">
         <svg xml:space="preserve" width="100%" height="100%">
            <text x="218" y="384" style="text-anchor: start; font-size: 8pt;">
               <a xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL]
                  xlink:href="{testblockref/xref/@href}">
                    <xsl:value-of select="@name"/>
               </a>
            </text>
         </svg>    
    </xsl:template>
    <xsl:template match="value-answer" mode="positionLast">
        <svg xml:space="preserve" width="100%" height="100%">
            <text x="79" y="464" style="text-anchor: start; font-size: 8pt;">
               <a xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL]
                  xlink:href="{testblockref/xref/@href}">
                    <xsl:value-of select="@name"/>
               </a>
            </text>
        </svg>
    </xsl:template>
</xsl:stylesheet>
 
Thanks, Jay.

Choosing the second suggestion (through templates) worked great! I found one more issue...forgetting that some HREF's would be nested within a "endblockref" rather than the (only) one that I supplied to you, "testblockref", is there a possibility for an "or" statement that will take either HREF instance whether it exists in either a "testblockref" or "endblockref"?

<a xmlns:xlink="<!-- testblockref or endblockref on xlink -->
xlink:href="{testblockref/xref/@href}">
<xsl:value-of select="@name"/>
</a>
 
Well...it's probably sloppy and/or poor coding, but I solved that last bit that I needed in order to pull either HREF from within a "testblockref" or an "endblockref". I simply used the "|" union operator. And since (in my case) there would never exist both types of refs (+ only one <xref> would exist anyway), it worked! Funny, huh? So the finished product was:

Code:
<a xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL] 
   xlink:href="{testblockref/xref/@href | endblockref/xref/@href}">
      <xsl:value-of select="@name"/>
</a>

Thanks for your help Jay. It was extremely helpful.

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top