Ravana
Ravana

Reputation: 415

XSLT wont manipulate XML file accordingly

This is the XML file:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="LabXSLT.xslt"?>
<orders>
    <order>
        <customerid>2364</customerid>
        <status>pending</status>
        <item instock="Y" itemid="SD93">
            <name>Flying By Roller Skates</name>
            <price>25.00</price>
            <qty>25</qty>
        </item>
        <item instock="N" itemid="B12">
            <name>Bounce-o Ball</name>
            <price>.35</price>
            <qty>150</qty>
        </item>
    </order>
    <order>
        <customerid>5268</customerid>
        <status>complete</status>
        <item instock="Y" itemid="Q52">
            <name>Crash N Burn Skis</name>
            <price>20</price>
            <qty>10</qty>
        </item>
    </order>
</orders>

XSLT file:

<xsl:for-each select="orders/order">
    <p>Customer Number:</p>
    <xsl:value-of select="customerid" />
    <p>Name:</p>
    <xsl:value-of select="name" />
</xsl:for-each>

The XSLT file is suppose to get custeomrID and name and display it. It gets the customerID but not the name. Its only left blank and it looks like it can only reach two of the name elements. I think one of the problems is that there are 2 elements in the element. No idea how to go about this. I cannot change the XML file also.

Upvotes: 0

Views: 150

Answers (6)

user357812
user357812

Reputation:

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="customerid|name">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    <xsl:template match="customerid/text()">
        <xsl:value-of select="concat('Customer Number: ',.)"/>
    </xsl:template>
    <xsl:template match="name/text()">
        <xsl:value-of select="concat('Name: ',.)"/>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

Output:

<p>Customer Number: 2364</p>
<p>Name: Flying By Roller Skates</p>
<p>Name: Bounce-o Ball</p>
<p>Customer Number: 5268</p>
<p>Name: Crash N Burn Skis</p>

Note: Pull style, pattern matching.

Upvotes: 0

Casey
Casey

Reputation: 1806

It sometimes helps to fix the indentation on your XML so you can see the hierarchy more clearly:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="LabXSLT.xslt"?>
<orders>
    <order>
        <customerid>2364</customerid>
        <status>pending</status>
        <item instock="Y" itemid="SD93">
            <name>Flying By Roller Skates</name>
            <price>25.00</price>
            <qty>25</qty>
        </item>
        <item instock="N" itemid="B12">
            <name>Bounce-o Ball</name>
            <price>.35</price>
            <qty>150</qty>
        </item>
    </order>
    <order>
        <customerid>5268</customerid>
        <status>complete</status>
        <item instock="Y" itemid="Q52">
            <name>Crash N Burn Skis</name>
            <price>20</price>
            <qty>10</qty>
        </item>
    </order>
</orders>

This makes it easier to see that you need to target item/name

Edit: To access each item in an order node, your XSL could look like this:

<xsl:for-each select="orders/order">
    <p>Customer Number:</p>
    <xsl:value-of select="customerid" />
    <xsl:for-each select="item">
        <p>Name:</p>
        <xsl:value-of select="name" />
    </xsl:for-each>
</xsl:for-each>

Upvotes: 2

LarsH
LarsH

Reputation: 28004

Ditto what @Casey said. However, if you use

<xsl:value-of select="item/name" />

you will only get the name of the first item, because that's what <xsl:value-of> does. Depending on your requirements, you may need to change that.

Upvotes: 0

Jayaram
Jayaram

Reputation: 839

since the name is under the tag item

your code should to print the name should be

<xsl: value-of select ="item/name"/>

Upvotes: 0

Lucero
Lucero

Reputation: 60276

The name tag is inside a item tag. So what shall be done when multiple names are on one customer?

Upvotes: 0

vamuso
vamuso

Reputation: 117

The name element is a child of the item. The doesn't appear to be a customer name element. Is this what you are trying to do, or do you want the item name?

Upvotes: 0

Related Questions