XSLT 使用命名空间转换 XML [英] XSLT Transform XML with Namespaces
本文介绍了XSLT 使用命名空间转换 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 XSLT 将一些 XML 转换为 HTML.
I'm trying to transform some XML into HTML using XSLT.
问题:
我无法让它工作.有人能告诉我我做错了什么吗?
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>
推荐答案
您需要在 xslt 中为要转换的元素提供命名空间前缀.出于某种原因(至少在 Java JAXP 解析器中),您不能简单地声明默认名称空间.这对我有用:
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>
这将捕获 XML 文档中命名空间的所有内容.
This will catch everything that is namespaced in your XML doc.
这篇关于XSLT 使用命名空间转换 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文