如何使用XLST从XML中删除某些属性

Jer*_*emy 6 regex xml xslt

我有一个通过Web服务返回给我的XML文档.

<Kronos_WFC encoding="ASCII" version="1.0" WFCVersion="6.1" TimeStamp="01/5/2011 8:38AM">
  <Response Status="Success" Timeout="1800" PersonKey="-1" Object="System" Username="1" Action="Logon" PersonNumber="1">
  </Response>
  <Response Status="Success" action="Load">
      <ScheduleGroup ScheduleGroupName="SomeName" AllowsInheritance="false" AllowContract="false" IsEmploymentTerm="false" />
      <ScheduleGroup ScheduleGroupName="GreatName" AllowsInheritance="true" AllowContract="false" IsEmploymentTerm="false" />
      <ScheduleGroup ScheduleGroupName="BestName" AllowsInheritance="true" AllowContract="false" IsEmploymentTerm="false" />
  </Response>
  <Response Status="Success" Object="System" Action="Logoff">
  </Response>
</Kronos_WFC>
Run Code Online (Sandbox Code Playgroud)

问题是我将结果转换为从此产品的xsd模式生成的业务对象(xsd2code).该产品在属性架构中没有任何内容(for Response):

  • 超时
  • PersonKey
  • 宾语
  • 用户名

我想做以下事情:

  • 删除上述属性
  • 将所有其他属性转换为元素,包括所有孩子,孩子的孩子等.

我如何使用XLST执行此操作.使用正则表达式删除不需要的属性会更简单吗?

Dim*_*hev 7

使用正则表达式删除不需要的属性会更简单吗?

不,这是一个非常简单的XSLT操作:

这种转变:

<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=
 "Response/@*[contains('|Timeout|PersonKey|Object|Username|',
                      concat('|',name(),'|')
                      )
            ]"/>
 <xsl:template match="@*">
  <xsl:element name="{name()}">
   <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当应用于提供的XML文档时:

<Kronos_WFC encoding="ASCII" version="1.0"
WFCVersion="6.1" TimeStamp="01/5/2011 8:38AM">
    <Response Status="Success" Timeout="1800" PersonKey="-1"
    Object="System" Username="1" Action="Logon"
    PersonNumber="1"></Response>
    <Response Status="Success" action="Load">
        <ScheduleGroup ScheduleGroupName="SomeName"
        AllowsInheritance="false" AllowContract="false"
        IsEmploymentTerm="false" />
        <ScheduleGroup ScheduleGroupName="GreatName"
        AllowsInheritance="true" AllowContract="false"
        IsEmploymentTerm="false" />
        <ScheduleGroup ScheduleGroupName="BestName"
        AllowsInheritance="true" AllowContract="false"
        IsEmploymentTerm="false" />
    </Response>
    <Response Status="Success" Object="System"
    Action="Logoff"></Response>
</Kronos_WFC>
Run Code Online (Sandbox Code Playgroud)

产生完全想要的,正确的结果:

<Kronos_WFC>
   <encoding>ASCII</encoding>
   <version>1.0</version>
   <WFCVersion>6.1</WFCVersion>
   <TimeStamp>01/5/2011 8:38AM</TimeStamp>
   <Response>
      <Status>Success</Status>
      <Action>Logon</Action>
      <PersonNumber>1</PersonNumber>
   </Response>
   <Response>
      <Status>Success</Status>
      <action>Load</action>
      <ScheduleGroup>
         <ScheduleGroupName>SomeName</ScheduleGroupName>
         <AllowsInheritance>false</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
      <ScheduleGroup>
         <ScheduleGroupName>GreatName</ScheduleGroupName>
         <AllowsInheritance>true</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
      <ScheduleGroup>
         <ScheduleGroupName>BestName</ScheduleGroupName>
         <AllowsInheritance>true</AllowsInheritance>
         <AllowContract>false</AllowContract>
         <IsEmploymentTerm>false</IsEmploymentTerm>
      </ScheduleGroup>
   </Response>
   <Response>
      <Status>Success</Status>
      <Action>Logoff</Action>
   </Response>
</Kronos_WFC>
Run Code Online (Sandbox Code Playgroud)

说明:

  1. 身份规则/模板"按原样"复制每个节点.

  2. 覆盖匹配与名称的任何属性的身份规则模板Timeout,PersonKey,Object,或Username具有空体,不复制它们(从输出的"删除"它们)

  3. 匹配任何属性的模板创建一个元素,其名称是匹配属性的名称,其文本node-child是匹配属性的值.

请记住:使用和覆盖标识规则是最基本和最强大的XSLT设计模式.