XSLT 使用命名空间转换 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1730875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSLT Transform XML with Namespaces
提问by Chris
I'm trying to transform some XMLinto HTMLusing XSLT.
我正在尝试使用XSLT将一些XML转换为HTML。
Problem:
问题:
I can't get it to work. Can someone tell me what I'm doing wrong?
我无法让它工作。有人能告诉我我做错了什么吗?
XML:
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:
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>
回答by Andy Gherna
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:
您需要在 xslt 中为要转换的元素提供命名空间前缀。出于某种原因(至少在 Java JAXP 解析器中),您不能简单地声明默认名称空间。这对我有用:
<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.
这将捕获 XML 文档中命名空间的所有内容。
回答by Steve
How do you execute the transformation? Maybe you forgot to link the XSLT stylesheet to XML document using:
你如何执行转换?也许您忘记使用以下命令将 XSLT 样式表链接到 XML 文档:
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
at the beginning of XML document. More explanation here.
在 XML 文档的开头。更多解释在这里。

