Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 28th, 2006, 04:33 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT change class attribute by ID?

This is my first dabbling into XSLT - I'm currently working on a web mapping tool that exports the data into SVG. I have an element

        <g id="LabelWebShields" gmwmsvg:typ="l" gmwmsvg:pri="2" class="g6E3n3r9q8">

that has an arbitrarily assigned class. I'm under the impression that I can NOT assign styles to the element using the ID as you can in XHTML (#elementID{color: blue;}). Assuming this is the case (if its not please let me know how!!!!) how do I

A) change the class to match a style class that I already have set up

 or

B) find the value of the class attribute by doing some sort of select and then dynamically build a style class to match the attribute value

 or

C) find the object by its ID and then change the style attribute

I beleive that either of these three options would take me to the same place I just have no clue how to get there! Any help would be MUCH appreciated as I have spent a good bit of time researching this only to get nowhere. Thanks!

 
Old March 1st, 2006, 08:54 AM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

B) //g[@class='g6E3n3r9q8'] would find g elements anywhere in the document that have a class attribute equal to g6E3n3r9q8. Putting @ in front of something says you're looking for a tag's attribute. You're going to have a bit of trouble doing anything "dynamically" in XSL; can you give an example of what you're starting with and what you would want to end up with and maybe a little on why?

C) Fairly similar: //[@id='LabelWebShields'] - I'll let someone else give you better XSL than I could, but you would basically want to pass the element through as-is and add a style attribute

<xsl:attribute name="style">
  color: blue;
</xsl:attribute>

 
Old March 1st, 2006, 09:03 AM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

here's what i have. .
<g id="LabelWebShields" gmwmsvg:typ="l" gmwmsvg:pri="2" class="g6E3n3r9q8">

here's what i want

<g id="LabelWebShields" gmwmsvg:typ="l" gmwmsvg:pri="2" class="g6E3n3r9q8" style="fill-opacity: 0.1; stroke-opacity: 0.1">

if you wouldn't mind could use this example to create the entire work flow . . .

ie: calling the template, setting the style etc. . . i've not had much luck getting this to work on my own

 
Old March 1st, 2006, 09:25 AM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is kind of simplified, but hopefully gives you an idea:

XML:

<?xml version="1.0"?>
<root>
    <g id="LabelWebShields" typ="l" pri="2" class="g6E3n3r9q8" />
</root>

XSL:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:gmwmsvg="urn:my-scripts">

  <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>

      <xsl:template match="//g[@id='LabelWebShields']">
          <g>
              <xsl:attribute name="id">
                  <xsl:value-of select="@id" />
              </xsl:attribute>
              <xsl:attribute name="style">
                  fill-opacity: 0.1; stroke-opacity: 0.1
              </xsl:attribute>
          </g>
      </xsl:template>


</xsl:stylesheet>

I haven't passed all your attributes through because I'm certain there's a better way to pass through your tag untouched and just add an attribute. I don't know how to do that, but hopefully someone else will jump in and tell you.

 
Old March 1st, 2006, 09:30 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You want something like

<xsl:template match="g">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:attribute name="style">fill-opacity: 0.1; stroke-opacity: 0.1</xsl:attribute>
  </xsl:copy>
</xsl:template>

This is assuming that you are processing the document as a whole in the usual XSLT "push" style, that is a recursive descent from the root applying templates at each level.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 1st, 2006, 10:26 AM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

my apologies. . . i had never even heard of xslt until tasked with this project. . . what i would like to do is change the class of this element or if that is not possible change the style. i have not been able to get the items you posted to work. i'm really unsure as to where they should placed etc. . . thanks in advance for any help!!!!

here is my xsl file

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:gmwmsvg="http://www.intergraph.com/GeoMedia/wmsvg"
>
<xsl:include href="Functions.xsl"/>
<xsl:include href="Elements.xsl"/>

<xsl:include href="Custom.xsl"/>

<xsl:param name="gwmtooltipfont" select="'Arial'"/>

<xsl:output method="xml" indent="yes" cdata-section-elements="svg:style" encoding="UTF-8"/>

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>


<xsl:variable name="HasLogo" select="svg:svg/svg:g[1]/svg:g[@id='_GWMLogo']"/>
<xsl:template match="@*|node()">
  <xsl:choose>
    <xsl:when test="@id='_GWMLogo'">
      <xsl:call-template name="GWMSelectedLayer"/>
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:when>
    <xsl:when test="@id='_GWMAll'">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:if test="count($HasLogo) = 0">
          <xsl:call-template name="GWMSelectedLayer"/>
        </xsl:if>
      </xsl:copy>
    </xsl:when>
    <xsl:when test="@id='gwmstyles'">

<xsl:call-template name="CustomDefs"/>

      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:call-template name="CustomStyles"/>
      </xsl:copy>
    </xsl:when>
    <xsl:otherwise>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="svg:svg">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="onload">_gwmOnSVGLoad();</xsl:attribute>
        <xsl:attribute name="onmouseover">_gwmMouseOver(evt);</xsl:attribute>
        <xsl:attribute name="onmouseout">_gwmMouseOut(evt);</xsl:attribute>
    <xsl:attribute name="onclick">_gwmMouseClick(evt);</xsl:attribute>
    <xsl:attribute name="onmousemove">_gwmMouseMove(evt);</xsl:attribute>
    <xsl:attribute name="onmousedown">_gwmMouseDown(evt);</xsl:attribute>
    <xsl:call-template name="Functions"/>
        <xsl:apply-templates select="node()"/>
        <xsl:call-template name="Redlines"/>
        <xsl:call-template name="DynamicGraphics"/>
        <xsl:call-template name="AlwaysOnTop"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>




Here is my custom.xsl file

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:gmwmsvg="http://www.intergraph.com/GeoMedia/wmsvg"
>

<xsl:template name="CustomDefs">

<defs>
 <filter id="DropShadow" filterUnits="objectBoundingBox" x="-50%" y="-50%" width="250%" height="250%" fill-opacity="0.1" stroke-opacity="0">
        <feGaussianBlur in="SourceAlpha" stdDeviation="60" result="BlurAlpha"/>
        <feOffset in="BlurAlpha" dx="100" dy="100" result="OffsetBlurAlpha"/>
        <feMerge>
            <feMergeNode in="OffsetBlurAlpha"/>
            <feMergeNode in="SourceGraphic"/>
        </feMerge>
    </filter>
</defs>
</xsl:template>

<xsl:template name="CustomStyles">
<![CDATA[
    .LPSymTRPOLE
    {
    marker-mid:url(#SymTRPOLE);
    stroke:#ff0ff0;
    fill:none;
    stroke-width:20;
    }


    .LabelWebShields
    {
        fill-opacity: .4;
        stroke-opacity: .4;
    }
]]>
</xsl:template>
</xsl:stylesheet>


Here is the SVG (xml) file (all elements removed except root/element i'm concerned with.

<?xml version="1.0"?>
<svg xml:space="preserve" id="gwmroot" preserveAspectRatio="xMidYMid meet" viewBox="0 0 16000 8500" style="stroke-linejoin:round" onload="_gwmOnSVGLoad();" onmouseover="_gwmMouseOver(evt);" onmouseout="_gwmMouseOut(evt);" onclick="_gwmMouseClick(evt);" onmousemove="_gwmMouseMove(evt);" onmousedown="_gwmMouseDown(evt);" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gmwmsvg="http://www.intergraph.com/GeoMedia/wmsvg">

<g id="_GWMAll" gmwmsvg:typ="a">
<g id="LabelWebShields" gmwmsvg:typ="l" gmwmsvg:pri="2" class="g6E3n3r9q8">
<use xlink:href="#g6E3n3r9q8" x="2107" y="5445"></use>
<use xlink:href="#g6E3n3r9q8" x="2620" y="5952"></use>
<use xlink:href="#g6E3n3r9q8" x="12116" y="6597"></use>
<use xlink:href="#g6E3n3r9q8" x="6074" y="6079"></use>
<use xlink:href="#g6E3n3r9q8" x="14027" y="7358"></use>
<use xlink:href="#g6E3n3r9q8" x="13917" y="6753"></use>
<use xlink:href="#g6E3n3r9q8" x="3707" y="3577"></use>
<use xlink:href="#g6E3n3r9q8" x="11051" y="5092"></use>
<use xlink:href="#g6E3n3r9q8" x="2015" y="2312"></use>
<use xlink:href="#g6E3n3r9q8" x="2123" y="945"></use>
<use xlink:href="#g6E3n3r9q8" x="6454" y="7832"></use>
<use xlink:href="#g6E3n3r9q8" x="13739" y="3013"></use>
<use xlink:href="#g6E3n3r9q8" x="12233" y="5501"></use>
<use xlink:href="#g6E3n3r9q8" x="9770" y="3427"></use>
<use xlink:href="#g6E3n3r9q8" x="2180" y="3527"></use>
<use xlink:href="#g6E3n3r9q8" x="13070" y="3470"></use>
<use xlink:href="#g6E3n3r9q8" x="10265" y="4251"></use>
<use xlink:href="#g6E3n3r9q8" x="12836" y="5150"></use>
<use xlink:href="#g6E3n3r9q8" x="8451" y="6724"></use>
<use xlink:href="#g6E3n3r9q8" x="2169" y="6778"></use>
<use xlink:href="#g6E3n3r9q8" x="1890" y="7480"></use>
<use xlink:href="#g6E3n3r9q8" x="10116" y="7196"></use>
<use xlink:href="#g6E3n3r9q8" x="2752" y="5530"></use>
</g>
</g>
</svg>

If you need to see anything else please let me know.

 
Old March 1st, 2006, 03:57 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Thanks for posting your code. Far too many people on this forum seem afraid of doing so, which makes it very hard to offer them advice.

Your general structure is good. I'd make one suggestion:

<xsl:template match="@*|node()">
  <xsl:choose>
    <xsl:when test="@id='_GWMLogo'">

let the templates do the choosing. Split each xsl:when into a separate template rule, for example this one would be

<xsl:template match="*[@id='_GWMLogo']">

At this point I'm having a bit of trouble relating your code to your problem description. That's at least in part because none of the @id values mentioned in the code actually seem to appear in your data. So let's go back to the problem.

<g id="LabelWebShields" gmwmsvg:typ="l" gmwmsvg:pri="2" class="g6E3n3r9q8">

A) change the class to match a style class that I already have set up

<xsl:template match="g[@id='LabelWebShields']">
<g>
  <xsl:copy-of select="@*"/>
  <xsl:attribute name="class">new class value</xsl:attribute>
  <xsl:apply-templates/>
</g>
</xsl:template>

B) find the value of the class attribute by doing some sort of select and then dynamically build a style class to match the attribute value

This description is a bit more troublesome because of the "then" - you are thinking in terms of an order of execution, not a functional relationship. Try to structure your code according to the output it has to produce: write a template for each piece of output, and in that template, gather all the input it needs from anywhere in the document. You haven't shown the output you are aiming at so it's hard to be more specific.

C) find the object by its ID and then change the style attribute

This looks like

<xsl:template match="g[@ID='some value']">
  <g>
   <xsl:copy-of select="@*"/>
   <xsl:attribute name="style">new style value</xsl:attribute>

i.e. exactly the same as A.

Note that you can copy all the attributes and then "overwrite" one of them.






Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 1st, 2006, 04:37 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Where would I place this template in my code?

The output I'm trying to get to is just:

<g id="LabelWebShields" gmwmsvg:typ="l" gmwmsvg:pri="2" class="MyClass">

from an input of

<g id="LabelWebShields" gmwmsvg:typ="l" gmwmsvg:pri="2" class="g6E3n3r9q8">

if this is not a placement issue then it must be a issue with the software that is generating the svg file. . . i have a call into tech support. do you know of any good resources for learning the general flow of what goes on in an XSL transformation etc?? I'm still having trouble understanding what exactly is going on (i didn't write most of that XSL - it came with the software). Thanks!

 
Old March 1st, 2006, 05:00 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The order of templates doesn't matter. What does matter is that the template actually gets invoked, as a result of an xsl:apply-templates call usually in the template that matches the parent node.

It sounds as if you need to do some background reading. There are some good books from Wrox ;=)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
'this.ID = id;' in class construction holf BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 October 6th, 2006 10:58 AM
XSL Attribute ID jonty XSLT 1 March 28th, 2006 03:53 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
Problem using XSLT to change XML attribute cbeech1980 XSLT 1 October 18th, 2005 06:26 AM
Change 1 attribute safin XSLT 1 October 3rd, 2005 12:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.