Home > Net >  XSL not returning values when applied to XML file
XSL not returning values when applied to XML file

Time:11-20

I have this XML file:

<?xml version="1.0"?>
<parks>
    <park>
        <park_name>MCGUANE (JOHN)</park_name>
        <acres>10.3</acres>
    
    </park>
    <park>
        <park_name>ARMOUR (PHILIP) SQUARE</park_name>
        <acres>9.05</acres>
    </park>
    <park>
        <park_name>FULLER (MELVILLE)</park_name>
        <acres>11.31</acres>
    </park>
    <park>
        <park_name>CORNELL (PAUL) SQUARE</park_name>
        <acres>8.8</acres>
    </park>
    <park>
        <park_name>RUSSELL (MARTIN) SQUARE</park_name>
        <acres>10.05</acres>
    </park>
    <park>
        <park_name>SHERMAN (JOHN)</park_name>
        <acres>57.69</acres>
    </park>
    <park>
        <park_name>DAVIS (DR. NATHAN) SQUARE</park_name>
        <acres>8.91</acres>
    </park>
</parks>

I created and used this XSL file to convert that XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <xsl:element name="results">
            <xsl:element name="names">
                <xsl:apply-templates select="parks/park">
                    <xsl:sort select="@name" order="ascending" />
                </xsl:apply-templates>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template match="park_name" >
        <xsl:element name="park">
            <xsl:attribute name="name">
                <xsl:value-of select="park_name" />
            </xsl:attribute>
        
            <xsl:attribute name="acres">
                <xsl:value-of select="acres" />
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

This is the file I receive once the original XML file is converted:

<?xml version="1.0" encoding="UTF-8"?>
<results>
    <names>
        <park name="" acres=""/>
        10.3
        <park name="" acres=""/>
        9.05
        <park name="" acres=""/>
        11.31
        <park name="" acres=""/>
        8.8
        <park name="" acres=""/>
        10.05
        <park name="" acres=""/>
        57.69
        <park name="" acres=""/>
        8.91
    </names>
</results>

I want the converted document to look like this and be sorted in alphabetical order:

<results>
    <names>
        <park name="PARK NAME" acres="99.99"/>
    </names>
</results>

I don't know why the name and acres attributes in the converted XML file will not store the values from the original XML file.

CodePudding user response:

You have the wrong context and selection in several places, use

 <xsl:apply-templates select="parks/park">
                <xsl:sort select="park_name" order="ascending" />
 </xsl:apply-templates>

and

<xsl:template match="park">
  <park name="{park_name}" acres="{acres}"/>
</xsl:template>

Also simply consider to use literal result elements (e.g. <results><names>...) instead of xsl:element (e.g. <xsl:element name="results"><xsl:element name="names">...) as the latter is only needed if you need to compute element names at runtime.

  • Related