How to create xsl file

Hello experts,

i have two xml files. First config.xml file looks like:
<?xml version="1.0"?>
<project>
  <counties>
    <county dist_id="2">
      <id>1</id>
      <short_name>county short name</short_name>
      <name>county name</name>
      <relative_value></relative_value>
      <interval></interval>
      <color>some hexadecimal color value</color>
    </county>
.
.
other county elements

  </counties>
</project>

map.xml file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<map>
  <counties>
    <county>
      <id>1</id>
      <d>M 70.5977,253.427 L 70.0034,255.667........(coordinates)</d>
.
.
.
other d elements
    </county>
 </counties>
</map>

And map.xsl file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:template match="map">
<svg width="800px" height="500px" xmlns="http://www.w3.org/2000/svg">
<g id="county">
<xsl:for-each select="counties/county">
      <xsl:element name="path">
            <xsl:attribute name="id">
                  <xsl:value-of select="id" />
            </xsl:attribute>
            <xsl:attribute name="d">
                  <xsl:value-of select="d" />
            </xsl:attribute>
            <xsl:attribute name="fill">#ffffff
            </xsl:attribute>
            <xsl:attribute name="stroke">#00B7EB
            </xsl:attribute>
            <xsl:attribute name="stroke-width">1.0
            </xsl:attribute>
            <xsl:attribute name="display">inline
            </xsl:attribute>
      </xsl:element>
</xsl:for-each>
</g>
</svg>
</xsl:template>
</xsl:transform>

I've created simple map.xsl file witch transform map.xml file to svg file. Like in my previous questions i'm not sure how to
copy color element text value from config.xml file to <xsl:attribute name="fill">#ffffff</xsl:attribute> element in map.xsl file in this for-each loop. When the color in config.xml is not defined  i want to let <xsl:attribute name="fill">#ffffff</xsl:attribute> element in white color like in example.


Please help me!
drahosAsked:
Who is Participating?
 
Geert BormansInformation ArchitectCommented:
Try this
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:variable name="config" select="document('config.xml')//counties"></xsl:variable>
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    <xsl:template match="map">
        
        <svg width="800px" height="500px" xmlns="http://www.w3.org/2000/svg">
            <g id="county">
                <xsl:for-each select="counties/county">
                    <xsl:variable name="this-id" select="id"/>
                    <xsl:element name="path">
                        <xsl:attribute name="id">
                            <xsl:value-of select="id" />
                        </xsl:attribute>
                        <xsl:attribute name="d">
                            <xsl:value-of select="d" />
                        </xsl:attribute>
                        <xsl:attribute name="fill">
                            <xsl:choose>
                                <xsl:when test="$config//county[id = $this-id]/color[normalize-space(.)]">
                                    <xsl:value-of select="$config//county[id = $this-id]/color"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:text>#ffffff</xsl:text>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                        <xsl:attribute name="stroke">#00B7EB</xsl:attribute>
                        <xsl:attribute name="stroke-width">1.0</xsl:attribute>
                        <xsl:attribute name="display">inline</xsl:attribute>
                    </xsl:element>
                </xsl:for-each>
            </g>
        </svg>
    </xsl:template>
</xsl:transform>

Open in new window

0
 
drahosAuthor Commented:
Thanks a lot for a such helping solutions!
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.