【发布时间】:2011-05-09 19:01:01
【问题描述】:
对这些问题的基本性质表示歉意 - 我是 XSLT 的新手(也是 Stack Overflow 的新手)。
我需要转换 Sharepoint Web 服务返回的以下 XML:
<GetGroupCollectionFromUser xmlns=
"http://schemas.microsoft.com/sharepoint/soap/directory/">
<Groups>
<Group ID="3" Name="Group1" Description="Description" OwnerID="1"
OwnerIsUser="False" />
<Group ID="15" Name="Group2" Description="Description"
OwnerID="12" OwnerIsUser="True" />
<Group ID="16" Name="Group3" Description="Description"
OwnerID="7" OwnerIsUser="False" />
</Groups>
</GetGroupCollectionFromUser>
进入这个:
<GetGroupCollectionFromUser xmlns=
"http://schemas.microsoft.com/sharepoint/soap/directory/">
<Groups>
<Group Name="Group1" />
<Group Name="Group2" />
<Group Name="Group3" />
</Groups>
</GetGroupCollectionFromUser>
基本上,我需要删除除 Name 之外的每个 Group 元素的所有属性。经过大量研究和修改,尤其是从原始 XML 中删除了命名空间声明,我想出了一些几乎完全符合我需要的东西:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:template match="/GetGroupCollectionFromUser">
<xsl:copy>
<xsl:apply-templates select="Groups" />
</xsl:copy>
</xsl:template>
<xsl:template match="Groups">
<xsl:copy>
<xsl:apply-templates select="Group" />
</xsl:copy>
</xsl:template>
<xsl:template match="Group">
<xsl:copy>
<xsl:apply-templates select="@Name" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但是,上面给出的不是 Name 属性,而是作为文本插入到 Group 元素中的 Name 属性的值,如下所示:
<GetGroupCollectionFromUser>
<Groups>
<Group>Group1</Group>
<Group>Group2</Group>
<Group>Group3</Group>
</Groups>
</GetGroupCollectionFromUser>
这最终将由期望以属性为中心的 XML 的第三方应用程序使用。我确定我遗漏了一些非常明显的东西,但无论我用它做什么,我似乎都无法只引入 Name 属性。两个问题:
如何更改 XSLT 以返回每个 Group 元素的 Name 属性,而不是其文本值?
还有,我该如何正确处理命名空间?当我将它包含在 XSLT 中时,根据我在此处和网络上其他地方找到的示例尝试了几种方法,但我什么也没得到。
提前感谢您的任何建议。
【问题讨论】: