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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML to XSLT

Status
Not open for further replies.

pr33tz

Technical User
Jul 5, 2006
4
GB
Hi all,

I've been working on a little XML to XSLT project (i'm new to it so i'm experimenting) and have come across a problem which is frustrating me. I have an XML file in the form of:

XML file:

<Orders>
<Order>
<Name>
<Address>
<TelNo>
<OrderTotal>
<Item>
<SuppCode>
<Desc>
<Price>
</Item>
<Item>
<SuppCode>
<Desc>
<Price>
</Item>
<Item>
<SuppCode>
<Desc>
<Price>
</Item>
</Order>
<Orders>

XSLT file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
<xsl:template match="/">
<HTML>
<BODY>
<font face="Arial" size="4">Purchase Orders</font>
<xsl:apply-templates />
</BODY>
</HTML>
</xsl:template>
<xsl:template match="/Orders">
<TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="1">
<TR bgcolor="#6090CF" >
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Name</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Address</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>TelNo</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>OrderTotal</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>SuppCode</STRONG></CENTER></font></TD>
<TD><font face="Arial" size="2" color="white"><CENTER><STRONG>Description</STRONG></CENTER></font></TD>
<TD><font face="Arial" size="2" color="white"><CENTER><STRONG>Price</STRONG></CENTER></font></TD>
</TR>
<xsl:for-each select="Order/Item">
<TR bgcolor="LightGreen">
<TD>
<font face="Arial" size="2"><xsl:value-of select="Name"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="Address"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="TelNo"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="OrderTotal"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="SuppCode"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="Desc"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="Price"/></font>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>

The "Item" node can occur many times but i can only get it to display once in XSLT. What should i be doing? I'd appreciate any help or any direction to guides/tutorials.

Thanks in advance.
 
looks like a logic error. You're looking for each order/item then calling items that are only in Order along With items that are only in Item.
The below shows the idea but its not very sound because instead of adding a new row for each item it adds a cell but it gets the point across

Code:
[b]<xsl:for-each select="Order">[/b]
<TR bgcolor="LightGreen">    
<TD>
<font face="Arial" size="2"><xsl:value-of select="Name"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="Address"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="TelNo"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="OrderTotal"/></font>
</TD>
[b]<xsl:for-each select="Item">[/b]
<TD>
<font face="Arial" size="2"><xsl:value-of select="SuppCode"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="Desc"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="Price"/></font>
</TD>
[b]</xsl:for-each>[/b]
</TR>
[b]</xsl:for-each>[/b]
</TABLE>    
</xsl:template>

Hope This Helps

MCP, .Net Solutions Development <%_%>
 
Hi,

Thanks for the reply.

I modified my XSL according to above but still no joy. The XSL file below is what i have and the pic shows how i want it to be displayed. Any ideas?

XSL file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="xmlns:por="URN:TEST.COM:purchaseOrder1">
<xsl:template match="/">
<HTML>
<BODY>
<font face="Arial" size="4">Purchase Order Details</font>
<xsl:apply-templates />
</BODY>
</HTML>
</xsl:template>

<xsl:template match="/por:Orders">
<TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="1">
<TR bgcolor="#6090CF" >
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>User</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Authorisor</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Workgroup</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>OrderNo</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Seq</STRONG></CENTER></font></TD>
</TR>


<xsl:for-each select="por:Order">
<TR bgcolor="LightGreen">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">
<xsl:value-of select="user:UpdateColor(0)" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="user:UpdateColor(1)" />
</xsl:eek:therwise>
</xsl:choose>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:UserName"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:Authorisor"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:Workgroup"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:OrderNo"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:OrderNoSeq"/></font>
</TD>

</TR>

</xsl:for-each>
</TABLE>



<TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="1">
<TR bgcolor="#6090CF" >
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Supplier</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Value</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Tax Code</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Tax Value</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>GL Code</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Item Code</STRONG></CENTER></font></TD>
<TD STYLE="fontface:Arial"><font color="white"><CENTER><STRONG>Desc</STRONG></CENTER></font></TD>
</TR>

<xsl:for-each select="por:Order/por:Item">
<TR bgcolor="LightGreen">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">
<xsl:value-of select="user:UpdateColor(0)" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="user:UpdateColor(1)" />
</xsl:eek:therwise>
</xsl:choose>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:SupplierCode"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:Value"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:TaxCode"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:TaxValue"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:GLCode"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:ItemCode"/></font>
</TD>
<TD>
<font face="Arial" size="2"><xsl:value-of select="por:Description"/></font>
</TD>

</TR>

</xsl:for-each>

</TABLE>
</xsl:template>
</xsl:stylesheet>

How i want it to be displayed:

my.php


Thanks again!!
 
are you still not getting any items after the first? or are your problems with where the data is displayed? Could you provide us with an xml?

MCP, .Net Solutions Development <%_%>
 
This is the XML file. It shows the data but it's all over the place, not like it should be as in the pic in my previous post.

<QLxEnvelope xmlns='URN:pr33tz.com:ENVELOPE'>

<CompanyCode>PR33TZ</CompanyCode>

<UserCode>DAB</UserCode>

<LedgerCode>PL1</LedgerCode>

<WorkgroupCode>BMORDER</WorkgroupCode>

<DocumentType>PURCHASEORDER</DocumentType>

<ViewingVersion>1</ViewingVersion>

<TransformVersion>1</TransformVersion>

<ImportVersion>1</ImportVersion>

<Description>Purchase Order</Description>

<DocumentValue>15000</DocumentValue>

<Message>

<Orders xmlns="URN:ABC.COM:pURCHASEORDER1">

<UserName>DCC</UserName>

<LedgerCode>PL1</LedgerCode>

<Workgroup>BMORDER</Workgroup>

<Order>

<Authorisor>RT08KG</Authorisor>

<UserName>JC08KG</UserName>

<Workgroup>BMORDER</Workgroup>

<OrderNo>BM042540</OrderNo>

<OrderNoSeq>1</OrderNoSeq>

<Item>

<SupplierCode>003600</SupplierCode>

<Value>15000</Value>

<TaxCode>1</TaxCode>

<TaxValue>2625</TaxValue>

<GLCode>BM.2.00.5115.C.-----</GLCode>

<ItemCode>SERVICE</ItemCode>

<Description>WHS ISSUES</Description>

<Quantity>1</Quantity>

</Item>

<Item>

<SupplierCode>00356</SupplierCode>

<Value>1000</Value>

<TaxCode>1</TaxCode>

<TaxValue>175</TaxValue>

<GLCode>BM.2.00.5115.C.-----</GLCode>

<ItemCode>SERVICE</ItemCode>

<Description>FENCING NEW GARDEN</Description>

<Quantity>1</Quantity>

</Item>

</Order>
<Order>

<Authorisor>RT08KG</Authorisor>

<UserName>PP00KG</UserName>

<Workgroup>BMORDER</Workgroup>

<OrderNo>BM042541</OrderNo>

<OrderNoSeq>1</OrderNoSeq>

<Item>

<SupplierCode>056400</SupplierCode>

<Value>8000</Value>

<TaxCode>1</TaxCode>

<TaxValue>1312</TaxValue>

<GLCode>BM.2.00.5456.C.-----</GLCode>

<ItemCode>SERVICE</ItemCode>

<Description>NEW SHOP SIGN</Description>

<Quantity>1</Quantity>

</Item>

<Item>

<SupplierCode>00792</SupplierCode>

<Value>1000</Value>

<TaxCode>1</TaxCode>

<TaxValue>175</TaxValue>

<GLCode>BM.2.00.5123.C.-----</GLCode>

<ItemCode>SERVICE</ItemCode>

<Description>WATER HOSE DELUXE 2000</Description>

<Quantity>1</Quantity>
</Item>
</Order>
</Orders>
</Message>
</Envelope>
 
It looks like your having trouble with the namespaces
you are using the same namespace in the xsl for both orders and QLxEnvelope

I made a quick xsl to test the principle by removing the namespaces from the xml and changing the close of the root element to </QLxEnvelope> ;) and had no problem looping through them

Code:
  <xsl:template match="/">
    <html>
      <body>
        <div id="orders">
          <xsl:for-each select="QLxEnvelope/Message/Orders/Order">
          Order Number:<xsl:value-of select="OrderNo" /><br />
          <xsl:for-each select="Item">
            Item Code: <xsl:value-of select="ItemCode" /> <br />

          </xsl:for-each>
          <hr />    
          </xsl:for-each>
        </div>
      </body>
    </html>
  </xsl:template>

I would definately look at your namespaces though




MCP, .Net Solutions Development <%_%>
 
Same Exmaple with the namespaces
Code:
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
  xmlns:env="URN:pr33tz.com:ENVELOPE" 
  xmlns:ordr="URN:ABC.COM:PURCHASEORDER1">
  <xsl:template match="/">
    <html>
      <body>
        <div id="orders">
          <xsl:for-each select="env:QLxEnvelope/env:Message/ordr:Orders/ordr:Order" >
              Order Number:<xsl:value-of select="ordr:OrderNo" /><br />
              <xsl:for-each select="ordr:Item">
                Item Code: <xsl:value-of select="ordr:ItemCode" /> <br />

              </xsl:for-each>
              <hr />
            </xsl:for-each>
        </div>
      </body>
    </html>
  </xsl:template>
  
</xsl:stylesheet>

MCP, .Net Solutions Development <%_%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top