2015-01-16 94 views
0

嗨我是新來的XSLT我想轉換Xml數據集,其中學生名稱映射從一個XML,其中有學生名稱和卷號的數據集。 classwise。XML數據集轉換

我想要使用xslt,但我不想添加所有標記只是要映射的列名稱標記,並將其他列按原樣添加。

這裏是輸入輸出數據集示例。 xml是來自c#代碼的正常dataset.writexml。

表1:

RollNo Sub1 Sub2 Sub3 Sub4 
1   65 89 67 34 
2   67 86 67 76 
3   86 67 78 45 
4   56 56 87 56 
5   76 56 56 78 

表2

Name  Sub1 Sub2 Sub3 Sub4 
Aman  65 89 67 34 
Ankit  67 86 67 76 
Om  86 67 78 45 
Narendra 56 56 87 56 
Faisal 76 56 56 78 

這是要用於轉化的的Xml

映射XML

<?xml version="1.0" standalone="yes"?> 
<School> 
    <Class Name="Class1"> 
     <StudentData Name="Aman" RollNo="1" /> 
     <StudentData Name="Ankit" RollNo="2" /> 
     <StudentData Name="Om" RollNo="3" /> 
     <StudentData Name="Narendra" RollNo="4" /> 
     <StudentData Name="Faisal" RollNo="5" />  
    </Class> 
    <Class Name="Class2"> 
     <StudentData Name="Abhinav" RollNo="1" /> 
     <StudentData Name="Abhishek" RollNo="2" /> 
     <StudentData Name="Ishaan" RollNo="3" /> 
     <StudentData Name="Mayank" RollNo="4" /> 
     <StudentData Name="Bhavana" RollNo="5" />  
    </Class> 
</School> 

XSLT創建至今

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" 
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://www.contoso.com"> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:template match="/"> 
    <DocumentElement> 
     <xsl:for-each select="//Comparision"> 
     <xsl:if test ="number(COL7)"> 
     <PositionMaster> 

        <xsl:variable name="RollNo" select="normalize-space(COL1)"/> 
        <xsl:variable name="Name"> 
         <xsl:value-of select="document('../../../MappingFiles/Mapping.xml')/School/PB[@Name='Class1']/TagData[@RollNo=$RollNo]/@Name"/> 
        </xsl:variable> 
        <Name> 
         <xsl:choose> 
          <xsl:when test="$Name!=''"> 
           <xsl:value-of select="$Name"/> 
          </xsl:when> 
          <xsl:otherwise> 
           <xsl:value-of select="$Name"/> 
          </xsl:otherwise> 
         </xsl:choose> 
        </Name> 
      </PositionMaster> 
     </xsl:if > 
     </xsl:for-each> 
    </DocumentElement> 
    </xsl:template> 
</xsl:stylesheet> 

感謝 阿曼

+4

顯示到目前爲止你已經嘗試過的東西。 – dario

+2

** 1。**請將您的輸入和輸出張貼爲XML代碼。 - ** 2。**轉換如何知道映射XML中的哪個'Class'用於查找? –

+0

@ michael.hor257k xslt將在其中硬編碼類名稱。 – XYZ

回答

2

只是作爲一個例子,下面給出輸入XML

<Table> 
    <Student> 
     <RollNo>1</RollNo> 
     <Sub1>11</Sub1> 
     <Sub2>12</Sub2> 
     <Sub3>13</Sub3> 
    </Student> 
    <Student> 
     <RollNo>2</RollNo> 
     <Sub1>21</Sub1> 
     <Sub2>22</Sub2> 
     <Sub3>23</Sub3> 
    </Student> 
    <Student> 
     <RollNo>3</RollNo> 
     <Sub1>31</Sub1> 
     <Sub2>32</Sub2> 
     <Sub3>33</Sub3> 
    </Student> 
    <Student> 
     <RollNo>4</RollNo> 
     <Sub1>41</Sub1> 
     <Sub2>42</Sub2> 
     <Sub3>43</Sub3> 
    </Student> 
    <Student> 
     <RollNo>5</RollNo> 
     <Sub1>51</Sub1> 
     <Sub2>52</Sub2> 
     <Sub3>53</Sub3> 
    </Student> 
</Table> 

,並命名爲 「Mapping.xml」 外部文件:

<?xml version="1.0" standalone="yes"?> 
<School> 
    <Class Name="Class1"> 
     <StudentData Name="Aman" RollNo="1" /> 
     <StudentData Name="Ankit" RollNo="2" /> 
     <StudentData Name="Om" RollNo="3" /> 
     <StudentData Name="Narendra" RollNo="4" /> 
     <StudentData Name="Faisal" RollNo="5" />  
    </Class> 
    <Class Name="Class2"> 
     <StudentData Name="Abhinav" RollNo="1" /> 
     <StudentData Name="Abhishek" RollNo="2" /> 
     <StudentData Name="Ishaan" RollNo="3" /> 
     <StudentData Name="Mayank" RollNo="4" /> 
     <StudentData Name="Bhavana" RollNo="5" />  
    </Class> 
</School> 

th E採用的樣式表:

XSLT 1.0

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

<xsl:param name="class">Class2</xsl:param> 

<xsl:key name="student" match="StudentData" use="concat(@RollNo, '|' , ../@Name)" /> 

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

<xsl:template match="RollNo"> 
    <Name> 
     <xsl:variable name="RollNo" select="." /> 
     <!-- switch context to the other document in order to use key --> 
     <xsl:for-each select="document('Mapping.xml')"> 
      <xsl:value-of select="key('student', concat($RollNo, '|' , $class))/@Name" /> 
     </xsl:for-each> 
    </Name> 
</xsl:template> 

</xsl:stylesheet> 

將返回:

<?xml version="1.0" encoding="UTF-8"?> 
<Table> 
    <Student> 
     <Name>Abhinav</Name> 
     <Sub1>11</Sub1> 
     <Sub2>12</Sub2> 
     <Sub3>13</Sub3> 
    </Student> 
    <Student> 
     <Name>Abhishek</Name> 
     <Sub1>21</Sub1> 
     <Sub2>22</Sub2> 
     <Sub3>23</Sub3> 
    </Student> 
    <Student> 
     <Name>Ishaan</Name> 
     <Sub1>31</Sub1> 
     <Sub2>32</Sub2> 
     <Sub3>33</Sub3> 
    </Student> 
    <Student> 
     <Name>Mayank</Name> 
     <Sub1>41</Sub1> 
     <Sub2>42</Sub2> 
     <Sub3>43</Sub3> 
    </Student> 
    <Student> 
     <Name>Bhavana</Name> 
     <Sub1>51</Sub1> 
     <Sub2>52</Sub2> 
     <Sub3>53</Sub3> 
    </Student> 
</Table> 
+0

謝謝,這有很大的幫助。 – XYZ