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!

XSLT transform problem

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
I know very little about xslt.
Here's my .xslt that transforms xml doc A into xml doc B:
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]
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="no"/> 
  <xsl:template match="/">
    <dXML>
      <RemoteUser>
	    <UserLogin>admin</UserLogin>
	    <UserAuthenticator>admin</UserAuthenticator>
      </RemoteUser>
      <xsl:apply-templates select="OrderCreate"/>
    </dXML>
  </xsl:template>
  <xsl:template match="OrderCreate">
   ...
  </xsl:template>
</xsl:stylesheet>

If my xml doc A contains a xmlns declaration:
Code:
<?xml version="1.0" encoding="UTF-8" ?> 
<OrderCreate xmlns="urn:cidx:names:DRAFT_0012:ces:schema:all:5:0" Version="5.0">
 ...
</OrderCreate>

The xml doc B is not created right - it's missing the whole OrderCreate section:
Code:
<dXML>
  <RemoteUser>
    <UserLogin>admin</UserLogin> 
    <UserAuthenticator>admin</UserAuthenticator> 
  </RemoteUser>
</dXML>

If I omit the xmlns declaration:
Code:
<?xml version="1.0" encoding="UTF-8" ?> 
<OrderCreate Version="5.0">
 ...
</OrderCreate>

the xml doc B does contain the 'OrderCreate' section.

It looks to me that the reason for that is the transform stylesheet is not finding a match on 'OrderCreate'. What can I do to fix that? Thank you.
 
You have been caught by a very common error. XPath does not use the default namespace.

The immediate symptom may be solved as follows:
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]
[COLOR=white blue]xmlns:indoc="urn:cidx:names:DRAFT_0012:ces:schema:all:5:0"
exclude-result-prefixes="indoc"[/color]>
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="no"/>
  <xsl:template match="/">
    <dXML>
      <RemoteUser>
        <UserLogin>admin</UserLogin>
        <UserAuthenticator>admin</UserAuthenticator>
      </RemoteUser>
      <xsl:apply-templates select="[COLOR=white blue]indoc:[/color]OrderCreate"/>
    </dXML>
  </xsl:template>
  <xsl:template match="[COLOR=white blue]indoc:[/color]OrderCreate">
   ...
  </xsl:template>
</xsl:stylesheet>

However, you probably need to review the requirements of your solution so that you can effect a correct result which includes proper namespace declarations.

BTW, this is such a common problem that it was fixed in XSLT 2.0. See here for information on the xpath-default-namespace attribute.

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top