Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL code not putting my nodes where i want

Status
Not open for further replies.

bananasbananas

IS-IT--Management
Apr 26, 2006
19
GB
how do i get the following structure for my Treeview:

TREENODES
- Item
-- NItem
-- NItem
- Item
-- NItem
- Item
-- NItem
-- NItem

etc


sample of MY XML:

<ItemsHierarchy>
- <Item ConceptId="0">
-- <Name>Business</Name>
-- <NarrowerItems>
--- <Item ConceptId = "238">
--- <Name>Abattoir</Name>
--- </Item>
--- <Item ConceptId = "234">
--- <Name>Pet Shops</Name>
--- </Item>
-- </NarrowerItems>
- </Item>
- <Item ConceptId="1">
-- <Name>Entertainment</Name>
-- <NarrowerItems>
--- <Item ConceptId = "148">
--- <Name>Clubs</Name>
--- </Item>
--- <Item ConceptId = "454">
--- <Name>Bars</Name>
--- </Item>
-- </NarrowerItems>
- </Item>
</ItemsHierarchy>

My current XSLT: (using 'esd' namespace)

<xsl:template match="*">

<TREENODES>

<xsl:for-each select="esd:Item">
<Item>
<xsl:attribute name="Name">

<xsl:value-of select="esd:Name/text()"/>

</xsl:attribute>

<xsl:for-each select="esd:NarrowerItems">

<NItem>
<xsl:attribute name="NName">

<xsl:value-of select="esd:Item/esd:Name/text()"/>

</xsl:attribute>

</NItem>

</xsl:for-each>

</Item>

</xsl:for-each>
<xsl:apply-templates />

</TREENODES>

</xsl:template>

AND the current output for my tree is:

TREENODES
- Item
-- NItem
- TREENODES
-- TREENODES
--- Item
---- NItem
--- TREENODES
---- TREENODES
----- Item
------ NItem

This isn't what i want, but how do i change my XSLT to correct the output?
 
Not exactly sure what you want... maybe this would be closer.
[tt]
<xsl:template match="/">
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="esd:ItemsHierarchy">
<TREENODES>
<xsl:for-each select="esd:Item">
<Item>
<xsl:attribute name="Name">
<xsl:value-of select="esd:Name/text()"/>
</xsl:attribute>
<xsl:for-each select="esd:NarrowerItems/esd:Item">
<NItem>
<xsl:attribute name="NName">
<xsl:value-of select="esd:Name/text()"/>
</xsl:attribute>
</NItem>
</xsl:for-each>
</Item>
</xsl:for-each>
[green]<!-- <xsl:apply-templates /> -->[/green]
</TREENODES>
</xsl:template>
[/tt]
 
Thankyou that produces the following:

TREENODES
- Item
-- NItem
-- NItem
-- NItem
-- NItem
-- NItem

(it only shows data for 1 Item)

Its close but I need to show data for all Items:

TREENODES
- Item
-- NItem
-- NItem
-- NItem
-- NItem
- Item
-- NItem
-- NItem
-- NItem
- Item
-- NItem
-- NItem
-- NItem

I'm wondering if we're doing a for-select on the wrong node, or I'm pulling the wrong value for the attribute...
 
Start with the sample xml, I got this.
[tt]
<TREENODES xmlns:esd="yournamespace">
<Item Name="Business">
<NItem NName="Abattoir" />
<NItem NName="Pet Shops" />
</Item>
<Item Name="Entertainment">
<NItem NName="Clubs" />
<NItem NName="Bars" />
</Item>
</TREENODES>
[/tt]
Is that not what you get?
 
Fraid not, but maybe I didn't provide a good enough example of the XML i'm using. Here is the full file:


And this is how I implimented your code:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="xmlns:xsi="xmlns:esd="xmlns="
<xsl:eek:utput method="xml"/>

<xsl:template match="/">
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="esd:ItemsHierarchy">
<TREENODES >
<xsl:for-each select="esd:Item">
<Item>
<xsl:attribute name="Name">
<xsl:value-of select="esd:Name/text()"/>
</xsl:attribute>
<xsl:for-each select="esd:NarrowerItems/esd:Item">
<NItem>
<xsl:attribute name="NName">
<xsl:value-of select="esd:Name/text()"/>
</xsl:attribute>
</NItem>
</xsl:for-each>
</Item>
</xsl:for-each>
</TREENODES>
</xsl:template>

</xsl:stylesheet>

And this is ALL I get once I've applied the XSLT to the treeview:


- TREENODE
-- Business
--- Agriculture and animal welfare
--- Hotels, catering and other accommodations
--- Information technology and telecom
--- Manufacturing - food
--- Manufacturing - non-food
--- Mining and quarrying
--- Not for profit organisations
--- Packing / importing
--- Personal services
--- Professional services
--- Property and real estate
--- Business services
--- Publishing and printing
--- Rental
--- Retail - food
--- Retail - non-food
--- Transport
--- Utilities
--- Wholesale - food
--- Wholesale - non-food
--- Construction and building services
--- Creative services and media
--- Education
--- Entertainment, leisure and tourism
--- Financial and legal services
--- Forestry
--- Health, social work and complementary medicines


I intend on using this on a businesses directory website ( )
 
hello, thankyou for your help, but I have sussed the code.

<xsl:template match="esd:ItemsHierarchy">
<TREENODES>
<xsl:apply-templates select="esd:Item">
<xsl:sort select="esd:Name"/>
</xsl:apply-templates>
</TREENODES>
</xsl:template>
<xsl:template match="esd:Item">
<Item>
<xsl:attribute name="Name">
<xsl:value-of select="esd:Name"/>
</xsl:attribute>
<xsl:apply-templates select="esd:NarrowerItems/esd:Item">
<xsl:sort select="esd:Name"/>
</xsl:apply-templates>
</Item>
</xsl:template>
 
Where are the other "Item"(s) in the output? If you have only one <Item> under the <ItemsHierarchy> you will get one, and the xslt script won't lie. Why not try the simplified xml source file to see what happen?
 
I hadn't read your 10:10 post when I replied. By reading it now, I would say it is good to take your own matter in your own hand, but don't complaint you get different thing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top