如何使用 XLST 从 XML 中删除某些属性
我通过 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>
问题是我将结果转换为从该产品的 xsd 模式生成的业务对象 (xsd2code
)。该产品在属性架构中没有任何内容(对于 Response
):
- Timeout
- PersonKey
- Object
- Username
我想执行以下操作:
- 删除上述属性
- 将所有其他属性转换为元素,包括所有子属性,并且孩子的孩子等等。
我如何使用 XLST 来做到这一点。使用正则表达式删除不需要的属性会更简单吗?
I have an XML document returned to me via a web service.
<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>
The problem is I turn the results into business objects generated from the xsd schema for this product (xsd2code
). The product has nothing in the schema for attributes (for Response
):
- Timeout
- PersonKey
- Object
- Username
I would like to do the following:
- Remove the above mentioned attributes
- Turn all other attributes into elements, including all children, and the children's childrens etc.
How do I do this using XLST. Would it be simpler to remove the unwanted attributes using Regex?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是一个非常简单的 XSLT 操作:
此转换:
应用于提供的 XML 文档时:
准确产生所需的正确结果:
说明:
身份规则/模板“按原样”复制每个节点。
覆盖与名称为
Timeout
、PersonKey
、Object
或Username
的任何属性匹配的身份规则的模板> 正文为空,并且不复制它们(从输出中“删除”它们)匹配任何属性的模板创建一个元素,其名称是匹配属性的名称,其文本node-child 是匹配属性的值。
记住:使用和覆盖身份规则 是最基本、最强大的 XSLT 设计模式。
No, this is a very simple XSLT operation:
This transformation:
when applied on the provided XML document:
produces exactly the wanted, correct result:
Explanation:
The identity rule/template copies every node "as-is".
The template overriding the identity rule that matches any attribute with name
Timeout
,PersonKey
,Object
, orUsername
has empty body and doesn't copy them ("deletes" them from the output)The template matching any attribute creates an element whose name is the name of the matched attribute and whose text node-child is the value of the matched attribute.
Remember: Using and overriding the identity rule is the most fundamental and powerful XSLT design pattern.