Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

XSLT Transform XML with Namespaces

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...