Click to See Complete Forum and Search --> : xmlns attribute in root element causes different XSLT results
plastic_wrapped
10-30-2006, 09:29 AM
Hi,
I have a program that contacts Amazon Web Services, and I want to use Xalan to apply XSLT files to simplify searches within the results. The XSLT works fine against sample XML saved locally, but when I pass in the contents of a URL, I get different results.
I discovered the difference is the sample XML doesn't have the xmlns attribute in the root element. When I copy the contents of the URL (which includes the xmlns attribute, below) to a file, I get the same (bad) results. Is there a variable in the Xalan objects I need to set?
Thanks in advance,
Elias
<?xml version="1.0" encoding="UTF-8"?>
<ItemLookupResponse xmlns="foo.bar">
<OperationRequest> ... etc
Charles
10-30-2006, 10:25 AM
You need the following in your stylesheet:<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
foobar:xhtml="http://www.w3.org/1999/xhtml">And then you would need to pre-pend a "foobar:" to each element from the namespace.
plastic_wrapped
10-30-2006, 11:54 AM
You need the following in your stylesheet:<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
foobar:xhtml="http://www.w3.org/1999/xhtml">And then you would need to pre-pend a "foobar:" to each element from the namespace.
Thanks for the advice. Just to be sure I understand what you mean by prepending each element, you mean I should use a setPrefix method (I'm using DOM) on the source XML? Right now I'm getting the source XML back as a string from URL.getContent and passing that string to Xalan.
Charles
10-30-2006, 12:27 PM
Actually, my example above got all screwed up cutting and pasting. Please post link to the XML.
plastic_wrapped
10-30-2006, 12:37 PM
Here's the XML. This is what I get back from URL.getContent():
<?xml version="1.0" encoding="UTF-8"?>
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
<OperationRequest>
<HTTPHeaders>
<Header Name="UserAgent" Value="Java/1.5.0_06"></Header>
</HTTPHeaders>
<RequestId>0302BTHCRESNZGS3FFMH</RequestId>
<Arguments>
<Argument Name="SearchIndex" Value="Music"></Argument>
<Argument Name="ItemId" Value="600308812925"></Argument>
<Argument Name="IdType" Value="UPC"></Argument>
<Argument Name="Condition" Value="New"></Argument>
<Argument Name="Service" Value="AWSECommerceService"></Argument>
<Argument Name="AWSAccessKeyId" Value="..."></Argument>
<Argument Name="Operation" Value="ItemLookup"></Argument>
</Arguments>
<RequestProcessingTime>0.0286579132080078</RequestProcessingTime>
</OperationRequest>
<Items>
<Request>
<IsValid>True</IsValid>
<ItemLookupRequest>
<Condition>New</Condition>
<IdType>UPC</IdType>
<ItemId>600308812925</ItemId>
<SearchIndex>Music</SearchIndex>
</ItemLookupRequest>
</Request>
<Item>
<ASIN>B000FBFSTW</ASIN>
<DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B000FBFSTW%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B000FBFSTW%253FSubscriptionId=1TP5ZJ87CB1JHYDA1E02</DetailPageURL>
<ItemAttributes>
<Artist>Mr. Lif</Artist>
<Manufacturer>Definitive Jux</Manufacturer>
<ProductGroup>Music</ProductGroup>
<Title>Mo' Mega</Title>
</ItemAttributes>
</Item>
</Items>
</ItemLookupResponse>
And the XSL I'm using:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/ItemLookupResponse">
<xsl:apply-templates select="Items"/>
</xsl:template>
<xsl:template match="Items">
<xsl:apply-templates select="Item"/>
</xsl:template>
<xsl:template match="Item">
<xsl:apply-templates select="ASIN"/>
</xsl:template>
<xsl:template match="ASIN">
<ASIN><xsl:value-of select="."/></ASIN>
</xsl:template>
</xsl:stylesheet>
Thanks again for your help.
Elias
Charles
10-30-2006, 12:50 PM
Give this a try:<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:awsec="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
<xsl:template match="/awsec:ItemLookupResponse">
<xsl:apply-templates select="awsec:Items"/>
</xsl:template>
<xsl:template match="awsec:Items">
<xsl:apply-templates select="awsec:Item"/>
</xsl:template>
<xsl:template match="awsec:Item">
<xsl:apply-templates select="awsec:ASIN"/>
</xsl:template>
<xsl:template match="awsec:ASIN">
<ASIN><xsl:value-of select="."/></ASIN>
</xsl:template>
</xsl:stylesheet>
plastic_wrapped
10-30-2006, 03:44 PM
Give this a try:
That did it, thanks. I'm a little unclear on why though. Could you tell me what aspect of namespaces I should educate myself about?
Thanks again,
Elias
Charles
10-30-2006, 03:49 PM
Once you have declared a namespace you change the whole element name. So "Item" is really "http://webservices.amazon.com/AWSECommerceService/2005-10-05:Item". That way it doesn't conflict with other Items. To make typing easier, though, you can set a default namespace, as done in your XML, or you can use an arbitrary handel, as I have done.