我有一个通过网络服务返回给我的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>
问题是我将结果转换为从此产品的xsd架构生成的业务对象(xsd2code
)。该产品在属性架构中没有任何内容(对于Response
):
我想做以下事情:
如何使用XLST执行此操作。使用正则表达式删除不需要的属性会更简单吗?
答案 0 :(得分: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>
应用于提供的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>
产生完全正确的结果:
<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>
<强>解释强>
身份规则/模板“按原样”复制每个节点。
覆盖与名称为Timeout
,PersonKey
,Object
或Username
的任何属性匹配的标识规则的模板具有空体并且不会复制它们(从输出中“删除”它们)
匹配任何属性的模板会创建一个元素,其名称是匹配属性的名称,其文本node-child是匹配属性的值。
请记住:使用和覆盖 the identity rule 是最基本,最强大的XSLT设计模式。