XSLT新手,尝试根据元素值消除重复的父级.但希望合并子级

  • 本文关键字:合并 希望 新手 元素 XSLT xslt-1.0
  • 更新时间 :
  • 英文 :


这是我的输入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分组和模板匹配。

相关内容

  • 没有找到相关文章

最新更新