English 中文(简体)
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>
最佳回答

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.

问题回答

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.





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签