Home > database >  XML file not displaying attributes after transformation
XML file not displaying attributes after transformation

Time:12-08

I am creating an XSL file that will take a parameter and display certain info from a XML document like so:

<?xml version="1.0" encoding="UTF-8"?>
<customer name="Evans, Terry"
      state="OH"
      zip="44660"
      orders="3"
      number_items="23"/>

The cid value for this person is c5391.

The XML file is this:

<?xml version="1.0" encoding="UTF-8"?>

<customers>
    <customer cid="c5391">
        <name>Evans, Terry</name>
        <street>641 Greenway Blvd.</street>
        <city>Mount Hope</city>
        <state>OH</state>
        <zip>44660</zip>
        <orders>
            <order oid="52517">
                <date>8/1/2017</date>
                <item iid="wb7133">
                    <description>Insulated Water Bottle</description>
                    <qty>2</qty>
                </item>
                <item iid="gps1015">
                    <description>Zendo GPS meter</description>
                    <qty>1</qty>
                </item>
                <item iid="bl2815">
                    <description>Boot Laces (Medium)</description>
                    <qty>1</qty>
                </item>
                <item iid="tr8140">
                    <description>Trail Mix (Pouch)</description>
                    <qty>5</qty>
                </item>
                <item iid="fa8442">
                    <description>First Aid Kit (Pack Size)</description>
                    <qty>1</qty>
                </item>
                <item iid="bb7117">
                    <description>Blister Patches</description>
                    <qty>3</qty>
                </item>
            </order>
            <order oid="53003">
                <date>8/5/2017</date>
                <item iid="hp7814">
                    <description>Fiberglass Light Hiking Poles (Spring Adj.)</description>
                    <qty>1</qty>
                </item>
            </order>
            <order oid="54814">
                <date>8/6/2017</date>
                <item iid="sb6601">
                    <description>Solar Battery Recharging Unit</description>
                    <qty>1</qty>
                </item>
                <item iid="br9002">
                    <description>Bug Repellent (Deep Woodes)</description>
                    <qty>2</qty>
                </item>
                <item iid="sb8502">
                    <description>Sunblock SPF 30 (Hiking Size)</description>
                    <qty>6</qty>
                </item>
            </order>
        </orders>
    </customer>

My XSL file is this:

<?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:param name="cid" select="'c5391'" />

    <xsl:variable name="custList" select="/customers/customer[@cid = $cid]" />

    <xsl:template match="/">
        <customer name="{name}"
            state="{state}"
            zip="{zip}"
            orders="count($custList/orders/order)"
            number_items="format-number(sum($custList/orders/order/item/qty), '##.##')" />
    </xsl:template>

</xsl:stylesheet>

My output file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<customer name=""
      state=""
      zip=""
      orders="count($custList/orders/order)"
      number_items="format-number(sum($custList/orders/order/item/qty), '##.##')"/>

I can't figure out why the values are not being picked up from the XML file. What change must I make to have my output file look like the example at the beginning of my post?

CodePudding user response:

Perhaps you want to simply match on the customer, instead of this:

<xsl:variable name="custList" select="/customers/customer[@cid = $cid]" />
<xsl:template match="/">
....

Perhaps you want this:

<xsl:template match="/customers/customer">
    <xsl:if test="@cid = $cid">
    ....

This would give you the customer, then output the content from there.

You have other issues in that template where {} are required. But this XSL:

<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:param name="cid" select="'c5391'" />
    <xsl:template match="/customers/customer">
        <xsl:if test="@cid = $cid">
        <customer name="{name}"
            state="{state}"
            zip="{zip}"
            orders="{count(orders/order)}"
            number_items="{format-number(sum(orders/order/item/qty), '##.##')}" />
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Yields this output:

<customer name="Evans, Terry" state="OH" zip="44660" orders="3" number_items="23"/>

CodePudding user response:

Turns out I simply needed to add $custList into the curly braces before the element I was trying to display. Huge brain fart. So, instead of this:

<xsl:template match="/">
    <customer name="{name}"
        state="{state}"
        zip="{zip}"
        orders="count($custList/orders/order)"
        number_items="format-number(sum($custList/orders/order/item/qty), '##.##')" />
</xsl:template>

I did this:

<xsl:template match="/">
    <customer name="{$custList/name}"
        state="{$custList/state}"
        zip="{$custList/zip}"
        orders="{count($custList/$ordList)}"
        number_items="{sum($custList/$ordList/item/qty)}" />
</xsl:template>

and I received my desired output.

  • Related