2016-10-20 11 views
0

余分な要素を持つ新しいXML文書に変換する必要がある次のXML文書があります。 (残りの部分は変更されません、私はシンプルさと読みやすさのための学生のセクションを追加しました):私は学生のセクションでは、次のように示しているので、大学名を抽出し、要素としてそれを挿入する必要が学生要素でXSLTを使用して要素を追加するXMLからXMLへ

<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>drumf[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"> 

私はIdentity ruleを使用してXSLTを使用していますが、私の大学名は空です。 提案がありますか?

<?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-複数ノードの値をチェックする –

答えて

1

あなたは、このような

として、親ノード studentの属性とキーを照合することによって、大学名を取得、あなたのテンプレート student/registrationから、このような

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

として学生IDのキーを使用することができます

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

関連する問題