51

I'm trying to transform some XML into HTML using XSLT.

Problem:

I can't get it to work. Can someone tell me what I'm doing wrong?

XML:

<ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.test.com/">
    <Brokerage>
        <BrokerageID>91</BrokerageID>
        <LastYodleeUpdate>0001-01-01T00:00:00</LastYodleeUpdate>
        <Name>E*TRADE</Name>
        <Validation i:nil="true" />
        <Username>PersonalTradingTesting</Username>
    </Brokerage>
</ArrayOfBrokerage>

XSLT:

<xsl:stylesheet version="1.0" xmlns="http://www.test.com/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xslFormatting="urn:xslFormatting">

    <xsl:output method="html" indent="no"/>

    <xsl:template match="/ArrayOfBrokerage">
        <xsl:for-each select="Brokerage">
            Test
       </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>
Rene Knop
  • 1,788
  • 3
  • 15
  • 27
Hungry Beast
  • 3,677
  • 7
  • 49
  • 75

2 Answers2

71

You need to provide a namespace prefix in your xslt for the elements you are transforming. For some reason (at least in a Java JAXP parser) you can't simply declare a default namespace. This worked for me:

<xsl:stylesheet version="1.0" xmlns:t="http://www.test.com/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xslFormatting="urn:xslFormatting">

    <xsl:output method="html" indent="no"/>

    <xsl:template match="/t:ArrayOfBrokerage">
        <xsl:for-each select="t:Brokerage">
            Test
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

This will catch everything that is namespaced in your XML doc.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
Andy Gherna
  • 2,135
  • 18
  • 22
  • This worked for me too in testing (running XSLT debug in Visual Studio 2008) – Murph Nov 13 '09 at 18:19
  • This did the trick. I had tried this with the combination of exclude-result-prefixes="t" because I thought it would allow me to not have to tack on t: before each node. Is there any way to avoid having to do this? – Hungry Beast Nov 13 '09 at 18:21
  • 1
    You could match elements using the local-name(), for instance: template match="/*[local-name()='ArrayOfBrokerage']" – Mads Hansen Nov 14 '09 at 01:26
  • thanks, this worked for me too. it's still a bit ungainly having to jam these namespaces in everywhere - wish there was a better way. – Steve Bennett Dec 09 '10 at 23:17
  • 12
    With xslt 2.0 you can use the xpath-default-namespace attribute in the stylesheet declaration, e.g. like ` ... ` – James Jul 16 '12 at 12:53
  • 2
    Might be worth noting that nested elements need namespace prefixes as well, e.g. ```match="/t:Brokerage/t:SomeChild/t:SomeGrandchild"``` – danialk Oct 19 '19 at 11:33
-2

How do you execute the transformation? Maybe you forgot to link the XSLT stylesheet to XML document using:

<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>

at the beginning of XML document. More explanation here.

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
Steve
  • 371
  • 4
  • 13
  • I'm not sure what you mean by this. I'm actually not doing a transform on an XML file but rather serializing a business object using the DataContractSerializer and specifying the namespace in the DataContract of the object. – Hungry Beast Nov 13 '09 at 18:09