2011-05-30 82 views
6

我有一个XML文档通过Web服务返回给我。如何使用XLST从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> 

的问题是我把成果转化为有关本产品(xsd2code)XSD模式生成的业务对象。该产品在架构没有为属性(Response):

  • 超时
  • PersonKey
  • 对象
  • 用户名

我想做到以下几点:

  • 删除上述属性
  • 将所有其他属性转换成元素,包括所有的孩子,和孩子们的儿童等

我如何做到这一点使用XLST。使用Regex删除不需要的属性会更简单吗?

+0

使用XSLT生成您需要的XML。要开始看到http://www.w3schools.com/xsl – nickytonline 2011-05-30 02:16:36

+0

好问题,+1。查看我的答案,获得完整,简短的解决方案和解释。这个解决方案非常优雅,因为它使用了最基础和最强大的XSLT设计模式 - 重写身份规则/模板。 – 2011-05-30 02:56:08

+0

美丽的+1 Thankyou – Jeremy 2011-05-30 03:57:26

回答

7

使用Regex删除 不需要的属性会更简单吗?

没有,这是一个很简单的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> 

时所提供的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> 

PR oduces正是想要的,正确的结果

<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> 

说明:

  1. 身份规则/模板拷贝的每一个节点 “原样”。

  2. 覆盖任何属性相匹配名称为TimeoutPersonKeyObject,或Username具有空体,不复制它们的身份规则模板(“删除”,从它们的输出)

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

记住:使用和压倒一切的the identity rule是最基本,最强大的XSLT设计模式。

+0

再次谢谢Dimitre! – Jeremy 2011-05-30 03:58:29

+0

@Jeremy孩子:不客气。 – 2011-05-30 04:07:50

相关问题