如何使用XLST从XML中删除某些属性
我有一个XML文档通过web服务返回给我如何使用XLST从XML中删除某些属性,xml,regex,xslt,Xml,Regex,Xslt,我有一个XML文档通过web服务返回给我 <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"> <
<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
)。产品在架构中没有任何属性(对于响应):
- 超时
- 私人钥匙
- 反对
- 用户名
我想做以下工作:
- 删除上述属性
- 将所有其他属性转换为元素,包括所有子项和子项的子项等
如何使用XLST实现这一点。使用正则表达式删除不需要的属性会更简单吗
是否要更简单地删除
使用正则表达式不需要的属性
不,这是一个非常简单的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>
<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>
应用于提供的XML文档时:
<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>
<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>
准确地生成所需的正确结果:
<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>
<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>
ASCII码
1
6.1
2011年5月1日上午8:38
成功
是最基本、最强大的XSLT设计模式。使用XSLT生成所需的XML。要开始,请参见好问题+1。请参阅我的答案,以获得完整、简短、简单的解决方案和解释。这个解决方案非常优雅,因为它使用了最基本、最强大的XSLT设计模式——重写标识规则/模板。