Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2009, 04:32 PM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Red face Not understanding namespaces

I am trying to help a collegue with some XML transformation. I have barely spent time in the XML-jungle, but being a "geek" I'm not afraid to throw myself into new technology, new to me that is.

So I'm trying to use this XML:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<Envelope>
  <Header>
    <MessageId>{1071FB4F-6F5F-42D8-92B2-4EF4D50C95F1}</MessageId>
    <SourceEndpointUser />
    <SourceEndpoint>DM1</SourceEndpoint>
    <DestinationEndpoint>DEMO</DestinationEndpoint>
    <Action>readSalesInvoice</Action>
    <RequestMessageId />
  </Header>
  <Body>
    <SalesInvoice xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/SalesInvoice">
      <DocPurpose>Original</DocPurpose>
      <SenderId>dm1</SenderId>
      <CustInvoiceJour class="entity">
        <Backorder>No</Backorder>
        <CashDisc>49.45</CashDisc>
        <CashDiscCode>5D2%</CashDiscCode>
        <CashDiscDate>2009-01-31</CashDiscDate>
        <CurrencyCode>NOK</CurrencyCode>
        <CustGroup>20</CustGroup>
        <XMLDocPurpose>Original</XMLDocPurpose>
        <CustInvoiceTrans class="entity">
          <CommissAmountCur>157.02</CommissAmountCur>
          <CommissAmountMST>157.02</CommissAmountMST>
          <CommissCalc>Yes</CommissCalc>
          <InventReportDimHistory class="entity">
            <InventDimId>1</InventDimId>
            <InventTransId>5904</InventTransId>
            <InventDim class="entity">
              <inventDimId>1</inventDimId>
              <RecVersion>1</RecVersion>
            </InventDim>
          </InventReportDimHistory>
        </CustInvoiceTrans>
        <TaxTrans class="entity">
          <AccountNum>2710</AccountNum>
          <CurrencyCode>NOK</CurrencyCode>
          <EUROTriangulation>No</EUROTriangulation>
          <Voucher>10000122</Voucher>
        </TaxTrans>
      </CustInvoiceJour>
    </SalesInvoice>
  </Body>
</Envelope>
and using this XSL as input to a transformation where I want the result to be HTML.

HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:fn="http://www.w3.org/2005/xpath-functions" 
	xmlns:n1="http://schemas.microsoft.com/dynamics/2006/02/documents/custinvoiceJour" 
	xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<xsl:output version="1.0" method="html" indent="yes" encoding="UTF-8" 
		doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN" 
		doctype-system="http://www.w3.org/TR/html4/loose.dtd"/>
	<xsl:template match="/">
		<xsl:value-of select="n1:SalesInvoice/SenderId" />
		<html>
			<head>
				<title>Test</title>
			</head>
			<body bgcolor="#FFFFFF">
				test: <xsl:value-of select="n1:SalesInvoice/n1:SenderId" />
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>
Unfortunately only the HTML markup gets out, not the "SenderId".
Can anyone please try to explain what the purpose of those namespaces are, and perhaps point out if I am doing something completely stupid here. ;-)

Thanks!
 
Old January 30th, 2009, 04:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're nearly there. In fact your problem isn't namespaces at all: it's context. When you do this:

<xsl:value-of select="n1:SalesInvoice/n1:SenderId" />

the context node is the document node, which doesn't have SalesInvoice (in any namespace) as a child. You need

select="Envelope/Body/n1:SalesInvoice/n1:SenderId"
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
skaue (January 31st, 2009)
 
Old January 31st, 2009, 07:44 AM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks. That makes sense. However when I tried this instead, it still did not print out expected result behind "test". I expected "dm1", but it's empty :-/

Here is my current xsl:

HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:fn="http://www.w3.org/2005/xpath-functions" 
                xmlns:n1="http://schemas.microsoft.com/dynamics/2006/02/documents/custinvoiceJour" 
                xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsl:output version="1.0" method="html" indent="yes" encoding="UTF-8" 
              doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN" 
              doctype-system="http://www.w3.org/TR/html4/loose.dtd"/>
  <xsl:template match="/">
    <html>
			<head>
				<title>
          Test</title>
			</head>
			<body bgcolor="#FFFFFF">
        test: 
        <xsl:value-of select="Envelope/Body/n1:SalesInvoice/n1:SenderId" />
    </body>
		</html>
	</xsl:template>
</xsl:stylesheet>
This is the output:

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:fn="http://www.w3.org/2005/xpath-functions" 
xmlns:n1="http://schemas.microsoft.com/dynamics/2006/02/documents/custinvoiceJour" 
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>
          Test</title>
  </head>
  <body bgcolor="#FFFFFF">
        test: 
        </body>
</html>
Not sure why it prints out the namespaces inside the HTML tag, but I guess it doesn't hurt.

The program I wrote to test this looks like this:
Code:
using System;
using System.Xml;
using System.Xml.Xsl;

public class XSLDemo
{
    [STAThread]
    static void Main(string[] args)
    {
        Transform("data.xml", "BCS.xsl");
    }
    public static void Transform(string sXmlPath, string sXslPath)
    {

        var xslt = new XslCompiledTransform();
        using (XmlReader src = XmlReader.Create(sXmlPath))
        {
            xslt.Load(sXslPath);
            XmlWriter result = XmlWriter.Create(Console.Out, xslt.OutputSettings);
            if (result != null) xslt.Transform(src, null, result, new XmlUrlResolver());
        }
        // Execute the transform and output the results to a file.
        xslt.Transform(sXmlPath, "books.html");
        Console.ReadKey();


    }
}
Thanks for helping! :-)
 
Old January 31st, 2009, 08:14 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can choose the prefix as you like but you need to ensure you use the same namespace as in the input XML. In the input XML you have http://schemas.microsoft.com/dynamic...s/SalesInvoice whereas in the stylesheet you have xmlns:n1="http://schemas.microsoft.com/dynamics/2006/02/documents/custinvoiceJour" so the namespace does not match.
Use xmlns:n1="http://schemas.microsoft.com/dynamics/2006/02/documents/SalesInvoice" and your stylesheet should output what you are looking for.

Some comments: you are using a version="1.0" stylesheet and you use XslCompiledTransform which is an XSLT 1.0 processor. That way it does not make much sense to use the XSLT/XPath 2.0 function namespace in your stylesheet. You can safely remove that.
And you might want to use exclude-result-prefixes on the xsl:stylesheet element to avoid that unused namespace declarations appear in the result document. If you need namespaces like http://www.w3.org/2001/XMLSchema at all in your stylesheet.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
skaue (January 31st, 2009)
 
Old January 31st, 2009, 11:10 AM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks. I can't believe I didn't see that namespace mismatch! I guess trying "anything" makes you almost blind for the small mistakes. Thanks!! It's transforming correctly. Next step is to use the complete html and not just this "testing"-xsl :-)

Again, thanks!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Namespaces and DLLs Akobold C# 0 March 18th, 2007 08:08 PM
Namespaces Amateur BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 November 27th, 2006 05:19 PM
namespaces anchal C# 1 July 3rd, 2006 02:53 PM
XML Namespaces billy_bob_the_3rd XML 1 January 31st, 2005 03:41 PM
NAMESPACES - WHAT ARE THEY? p_nut33 C# 2 July 31st, 2003 03:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.