【发布时间】:2011-09-05 12:45:22
【问题描述】:
我觉得这个问题很简单,但我已经有好几年没有做任何 xslt 了,所以也许有人可以帮忙?
我有一段由 .net 类 DataContractSerializer 生成的 xml,我需要使用 xslt 从该 xml 中提取数据以得到一些 html。对我来说,让事情变得复杂的是命名空间的大量使用......
xml 的 sn-p 如下所示:
<FundDeal xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Deal">
<Id xmlns="http://schemas.datacontract.org/2004/07/Guide.BusinessObjects.Deal">DEAL12345</Id>
<Account xmlns:d2p1="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Account">
<d2p1:AlternateId i:nil="true"/>
<d2p1:Designation>XXX</d2p1:Designation>
<d2p1:Name>QWERTY</d2p1:Name>
<d2p1:Number>12345678</d2p1:Number>
<d2p1:Status i:nil="true"/>
</Account>
<Agent xmlns:d2p1="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Account">
<d2p1:Id>54321</d2p1:Id>
<d2p1:Name>ASDFG</d2p1:Name>
<d2p1:Status>Active</d2p1:Status>
</Agent>
....
</FundDeal>
现在,我需要通过样式表来转换这个 xml,我发现这很困难。我认识到 xsl 需要自己对所涉及的命名空间的引用,并且可以使用以下 xsl 轻松提取上面的 Deal Id 之类的内容:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:grbd="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Deal"
xmlns:gbd="http://schemas.datacontract.org/2004/07/Guide.BusinessObjects.Deal"
xmlns:grba="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Account">
<xsl:output indent="yes" omit-xml-declaration="yes" method="html"/>
<xsl:template match="/">
<html>
<head>
<!-- some styles here -->
</head>
<body>
<table cellpadding="5" cellspacing="5" border="0">
<tr>
<td class="SectionTitle" colspan="2">
<xsl:text>Deal Cancellation Notification - </xsl:text>
<xsl:value-of select="//ggbd:Id"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
但我正在努力阅读诸如帐户名称之类的内容,因为似乎有多个命名空间正在进行。
谁能告诉我访问 (a) 帐户名称和 (b) 代理名称的 xpath 吗?我认为了解如何访问这些可能会让我访问我需要的所有其他内容。
非常感谢, 皮特
【问题讨论】:
标签: xml xslt namespaces transform xml-namespaces