XSLT:防止相同元素多次出现

时间:2016-06-10 15:53:51

标签: xslt

我正在尝试使用XSLT来处理多个文件。需要使用另一个文件的内容修改一个文件中的某些内容。

例如,在一个文件中查找元素名称“type”,并将其内容替换为名为“targetSiteCode”的元素的内容,依此类推。

这是第一个文件(我只展示了可能的最小工作示例):

<?xml version="1.0" encoding="UTF-8"?>
<Document>
    <component>

        <entryRelationship>
            <effectiveTime value="first time"/>                                 
            <targetSiteCode code="site" codeSystem="firstcod_esys"  codeSystemName="firstcode_name" displayName="firstcode_disp"/>
        </entryRelationship>

        <entryRelationship>
            <effectiveTime value="second time"/>
            <targetSiteCode code="LACF" 
        codeSystem="second_code_sys" codeSystemName="second_code_name" displayName="second_code_disp"/>
        </entryRelationship>                                                                

    </component>                        
</Document>

和名为“File2.xml”的第二个文件:

<?xml version="1.0" encoding="UTF-8"?>
<Sample>
    <receivedTime value="2016-04-04T07:03:00Z"/>
    <collect>
        <type>      
            <coding>
                <system value="somesystem"/>
                <code value="somecode"/>
                <display value="somedisplay"/>
            </coding>
        </type> 
    </collect>
</Sample>

这是我的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*" />

    <xsl:variable name="input" select="/" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/">
        <Bundle>            
            <entry>
                <fullUrl value ="somevalue"/>           
                <resource>                          
                        <xsl:apply-templates select= "Document/component/entryRelationship"/>
                </resource>
            </entry>
        </Bundle>
    </xsl:template>

    <xsl:template match = "Document/component/entryRelationship">
            <xsl:apply-templates select="document('File2.xml')/*"/>             
    </xsl:template>

    <xsl:template match = "Sample/receivedTime">
        <receivedTime>
            <xsl:apply-templates select = "$input/Document/component/entryRelationship/effectiveTime"/>
        </receivedTime>
    </xsl:template>

    <xsl:template name="effectiveTime">
        <time>
            <xsl:attribute name="value">
                <xsl:value-of select="@displayName" />
            </xsl:attribute>
        </time>
    </xsl:template>     

    <xsl:template match = "Sample/collect/type">
        <type>
            <xsl:apply-templates select = "$input/Document/component/entryRelationship/targetSiteCode"/>
        </type>
    </xsl:template>     

    <xsl:template match="targetSiteCode">   
        <coding>
            <system >
                <xsl:attribute name="value">
                    <xsl:value-of select="@codeSystem" />
                </xsl:attribute>
            </system>
            <code>
                <xsl:attribute name="value">
                    <xsl:value-of select="@code"/>
                </xsl:attribute>
            </code>
            <display>
                <xsl:attribute name="value">
                    <xsl:value-of select="@displayName" />
                </xsl:attribute>
            </display>
        </coding>
    </xsl:template>         
</xsl:stylesheet>

这就是我得到的:

<?xml version="1.0" encoding="UTF-8"?>
<Bundle>
   <entry>
      <fullUrl value="somevalue"/>
      <resource>
         <Sample>
            <receivedTime>
               <effectiveTime value="first time"/>
               <effectiveTime value="second time"/>
            </receivedTime>
            <collect>
               <type>
                  <coding>
                     <system value="firstcod_esys"/>
                     <code value="site"/>
                     <display value="firstcode_disp"/>
                  </coding>
                  <coding>
                     <system value="second_code_sys"/>
                     <code value="LACF"/>
                     <display value="second_code_disp"/>
                  </coding>
               </type>
            </collect>
         </Sample>
         <Sample>
            <receivedTime>
               <effectiveTime value="first time"/>
               <effectiveTime value="second time"/>
            </receivedTime>
            <collect>
               <type>
                  <coding>
                     <system value="firstcod_esys"/>
                     <code value="site"/>
                     <display value="firstcode_disp"/>
                  </coding>
                  <coding>
                     <system value="second_code_sys"/>
                     <code value="LACF"/>
                     <display value="second_code_disp"/>
                  </coding>
               </type>
            </collect>
         </Sample>
      </resource>
   </entry>
</Bundle>

如您所见,effectiveTimecoding都出现了两次。这就是我想要的:

<?xml version="1.0" encoding="UTF-8"?>
<Bundle>
   <entry>
      <fullUrl value="somevalue"/>
      <resource>
         <Sample>
            <receivedTime>
               <effectiveTime value="first time"/>            
            </receivedTime>
            <collect>
               <type>
                  <coding>
                     <system value="firstcod_esys"/>
                     <code value="site"/>
                     <display value="firstcode_disp"/>
                  </coding>                  
               </type>
            </collect>
         </Sample>
         <Sample>
            <receivedTime>               
               <effectiveTime value="second time"/>
            </receivedTime>
            <collect>
               <type>                  
                  <coding>
                     <system value="second_code_sys"/>
                     <code value="LACF"/>
                     <display value="second_code_disp"/>
                  </coding>
               </type>
            </collect>
         </Sample>
      </resource>
   </entry>
</Bundle>

0 个答案:

没有答案