XML到XML使用XSLT添加元素

问题描述:

我有以下XML文档,我需要将其转换为具有额外元素的新XML文档。XML到XML使用XSLT添加元素

<doc> 
     <colleges>   
      <college>   
       <college-name>harvard</college-name>   
        <members> 
         <!-- College staff--> 
         <staff>  
          <employee id="2200010001" contract="full-time"/>        
          <employee id="2200010002" contract="temp"/> 
         </staff> 
         <!-- College students --> 
         <students> 
          <student id="1000020001"/> 
          <student id="1000020003"/> 
         </students> 
        </members> 
      </college> 
      <college>   
       <college-name>wharton</college-name>   
        <members> 
         <!-- College staff--> 
         <staff>  
          <employee id="2200010003" contract="full-time"/>        
          <employee id="2200010004" contract="temp"/> 
         </staff> 
         <!-- College students --> 
         <students>        
          <student id="1000020002"/> 
         </students> 
        </members> 
       </college> 
     </colleges> 
     <students>     
      <student id="1000020001">   
       <personal>    
        <name> 
         <firstname>Hillary</firstname> 
         <lastname>Clinton</lastname> 
        </name>      
        <!-- Student contact information --> 
        <contact-information> 
         <phone>+12123004000</phone> 
         <email>[email protected]</email> 
        </contact-information> 
       </personal> 
       <registration> 
        <!-- Student university information -->          
        <degree> 
         <type>undergrad</type> 
         <status>1</status> 
         <matriculated>yes</matriculated>      
        </degree> 
        <degree> 
         <type>masters</type> 
         <status>1</status> 
         <matriculated>yes</matriculated>      
        </degree> 
       </registration> 
      </student"> 
      <student id="1000020002"> 
       <personal>    
        <name> 
         <firstname>Donald</firstname> 
         <lastname>Trump</lastname> 
        </name> 
        <!-- Student contact information --> 
        <contact-information> 
         <phone>+12123004001</phone> 
         <email>[email protected]</email> 
        </contact-information> 
       </personal> 
       <registration> 
        <!-- Student university information -->          
        <degree> 
         <type>undergrad</type> 
         <status>1</status> 
         <matriculated>yes</matriculated>      
        </degree> 
        <degree> 
         <type>masters</type> 
         <status>1</status> 
         <matriculated>yes</matriculated>      
        </degree> 
       </registration> 
      </student"> 
     </students> 
    </doc> 

学生元素我需要提取的大学名称和将它插入元素,让学生部分显示如下:(其余部分是不变的,我只是说了简单性和可读性助学款)

<student id="1000020001">   
     <personal>    
      <name> 
       <firstname>Hillary</firstname> 
       <lastname>Clinton</lastname> 
      </name>      
      <!-- Student contact information --> 
      <contact-information> 
       <phone>+12123004000</phone> 
       <email>[email protected]</email> 
      </contact-information> 
     </personal> 
      <registration> 
       <!-- Student university information --> 
       <college-name>harvard</college-name>    
       <degree> 
        <type>undergrad</type> 
        <status>1</status> 
        <matriculated>yes</matriculated>      
       </degree> 
       <degree> 
        <type>masters</type> 
        <status>1</status> 
        <matriculated>yes</matriculated>      
       </degree> 
      </registration> 
    </student"> 

我正在使用XSLT,使用Identity rule,但我的大学名称是空的。 有什么建议吗?

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0"> 
    <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> 
    <!-- Student registration--> 
    <xsl:template match="student/registration"> 
     <registration> 
      <xsl:apply-templates select="@* | *"/> 
      <xsl:element name="college-name"> 
       <xsl:call-template name="extractCollegeName"> 
         <xsl:with-param name="student-id" select="student/@id"/> 
       </xsl:call-template> 
      </xsl:element> 
     </registration> 
    </xsl:template> 

    <!-- check student id for each college --> 
    <xsl:template name="extractCollegeName"> 
     <xsl:param name="student-id"/> 
     <xsl:for-each select="/doc/colleges/college"> 
      <xsl:if test="$student-id = members/students/student/@id"> 
       <xsl:value-of select="college-name"/> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 
+1

:http://stackoverflow.com/questions/40104423/xslt-检查多个节点中的值 –

您可以使用学生证等

<xsl:key name="coll-name" match="college//student" use="@id"/> 

的关键从你的模板student/registration,通过匹配与父节点student的属性关键拿到大学名称,如

key('coll-name', ancestor::student/@id) 

然后通过使用xpath得到college-name的值ancestor::college/college-name

整体样式:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:key name="coll-name" match="college//student" use="@id"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 
    <!-- Student registration--> 
    <xsl:template match="student/registration"> 
     <registration> 
      <xsl:element name="college-name"> 
       <xsl:value-of select="key('coll-name', ancestor::student/@id)/ancestor::college/college-name"/> 
      </xsl:element> 
      <xsl:apply-templates select="@* | *"/> 
     </registration> 
    </xsl:template> 

</xsl:stylesheet> 

看到它你为什么不使用**键如下所示**工作here

+0

这很好。谢谢 – spicyramen