这是我的输入xml,希望删除<SRNumber>1</SRNumber>
,但合并childServiceRequest_LXKCTrackCodes
。感谢任何帮助,让我在这件事上崩溃了一天输入XML:
<ServiceRequest> <SRNumber>1</SRNumber>
<ServiceRequest_LXKCTrackCodes>
<Note>Test1</Note>
</ServiceRequest_LXKCTrackCodes>
</ServiceRequest>
<ServiceRequest>
<SRNumber>1</SRNumber> <ServiceRequest_LXKCTrackCodes> <Note>Test2</Note> </ServiceRequest_LXKCTrackCodes> </ServiceRequest> <ServiceRequest> <SRNumber>2</SRNumber> <ServiceRequest_LXKCTrackCodes> <Note>Test3</Note> </ServiceRequest_LXKCTrackCodes> </ServiceRequest>
所需输出:
<ServiceRequest>
<SRNumber>1</SRNumber>
<ServiceRequest_LXKCTrackCodes>
<Note>Test1</Note>
</ServiceRequest_LXKCTrackCodes>
<ServiceRequest_LXKCTrackCodes>
<Note>Test2</Note>
</ServiceRequest_LXKCTrackCodes>
<ServiceRequest>
<SRNumber>2</SRNumber>
<ServiceRequest_LXKCTrackCodes>
<Note>Test3</Note>
</ServiceRequest_LXKCTrackCodes>
</ServiceRequest>
------------------------------------------------------------------------
此转换:
<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:key name="kSRByNum" match="ServiceRequest" use="SRNumber"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"ServiceRequest
[generate-id()
=
generate-id(key('kSRByNum', SRNumber)[1])
]
">
<xsl:copy>
<xsl:apply-templates select="SRNumber"/>
<xsl:apply-templates select=
"key('kSRByNum', SRNumber)/ServiceRequest_LXKCTrackCodes"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ServiceRequest"/>
</xsl:stylesheet>
应用于所提供的XML时(经过更正以使其格式正确):
<t>
<ServiceRequest>
<SRNumber>1</SRNumber>
<ServiceRequest_LXKCTrackCodes>
<Note>Test1</Note>
</ServiceRequest_LXKCTrackCodes>
</ServiceRequest>
<ServiceRequest>
<SRNumber>1</SRNumber>
<ServiceRequest_LXKCTrackCodes>
<Note>Test2</Note>
</ServiceRequest_LXKCTrackCodes>
</ServiceRequest>
<ServiceRequest>
<SRNumber>2</SRNumber>
<ServiceRequest_LXKCTrackCodes>
<Note>Test3</Note>
</ServiceRequest_LXKCTrackCodes>
</ServiceRequest>
</t>
生成所需的正确结果:
<t>
<ServiceRequest>
<SRNumber>1</SRNumber>
<ServiceRequest_LXKCTrackCodes>
<Note>Test1</Note>
</ServiceRequest_LXKCTrackCodes>
<ServiceRequest_LXKCTrackCodes>
<Note>Test2</Note>
</ServiceRequest_LXKCTrackCodes>
</ServiceRequest>
<ServiceRequest>
<SRNumber>2</SRNumber>
<ServiceRequest_LXKCTrackCodes>
<Note>Test3</Note>
</ServiceRequest_LXKCTrackCodes>
</ServiceRequest>
</t>
解释:适当使用Muenchian分组和模板匹配。