簡體   English   中英

如何使用XSLT復制特定屬性?

[英]How to copy specific attribute using XSLT?

對這些問題的基本性質表示道歉 - 我對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>

但是,上面給出了作為文本插入到Group元素中的Name屬性的值,而不是Name屬性,如下所示:

<GetGroupCollectionFromUser>
    <Groups>
        <Group>Group1</Group>
        <Group>Group2</Group>
        <Group>Group3</Group>
    </Groups>
</GetGroupCollectionFromUser>

這最終將由期望以屬性為中心的XML的第三方應用程序使用。 我確定我錯過了一些令人尷尬的東西,但無論我用它做什么,我似乎都無法僅僅引入Name屬性。 兩個問題:

如何更改XSLT以返回每個Group元素的Name屬性,而不是其值作為文本?

而且,我如何正確處理命名空間? 當我將它包含在XSLT中時,嘗試基於我在這里和網上其他地方找到的示例的幾種方法,我什么也得不到。

提前感謝任何建議。

處理這些情況的方法是覆蓋Identity Transform:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msft="http://schemas.microsoft.com/sharepoint/soap/directory/">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="msft:Group">
        <xsl:copy>
            <xsl:apply-templates select="@Name" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

輸出:

<GetGroupCollectionFromUser
    xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
    <Groups>
        <Group Name="Group1" />
        <Group Name="Group2" />
        <Group Name="Group3" />
    </Groups>
</GetGroupCollectionFromUser>

請注意,輸出正確命名空間。

該解決方案因其簡單性和靈活性而是優選的。 除非存在指定備用行為的覆蓋,否則將按原樣復制所有節點和屬性。

我認為如果你不在這里使用xsl:copy ,它會變得更容易。 嘗試這個:

<xsl:template match="Groups/*">
 <xsl:element name="Group">
     <xsl:attribute name="Name">
      <xsl:value-of select="@Name" />
     </xsl:attribute>
 </xsl:element>
</xsl:template>

對第二個問題的回答是FAQ 聲明命名空間並在xsl中使用聲明的前綴。 最終解決方案

<xsl:stylesheet version="1.0" 
    xmlns:myns="http://schemas.microsoft.com/sharepoint/soap/director/"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output method="xml" encoding="utf-8" indent="no"/>

 <xsl:template match="/myns:GetGroupCollectionFromUser">
  <Groups>                 
    <xsl:apply-templates select="myns:Groups" />
  </Groups>
 </xsl:template>

 <xsl:template match="myns:Groups/*">
  <xsl:element name="Group">
     <xsl:attribute name="Name">
      <xsl:value-of select="@Name" />
     </xsl:attribute>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

這可能是正確產生所需結果的最短轉換之一

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@*[not(name()='Name')]"/>
</xsl:stylesheet>

應用於提供的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>

說明

  1. 身份規則(模板)“按原樣”復制每個節點。

  2. 只有一個模板可以覆蓋標識規則 它匹配名稱不是“Name”的任何屬性。 模板的主體為空,這導致未復制任何匹配的屬性。

使用和覆蓋標識規則是最基本和最強大的XSLT設計模式。 在這里閱讀它

這似乎對我有用:

<xsl:template match="Group">
    <xsl:copy>
        <xsl:attribute name="Name">
            <xsl:apply-templates select="@Name" />
        </xsl:attribute>
    </xsl:copy>
</xsl:template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM