XML 数据集转换

XML dataset transformation

嗨,我是 XSLT 的新手,我想转换 Xml 数据集,其中学生姓名映射自 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>

谢谢 阿曼

举个例子,给定以下输入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>

以下样式表:

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>

将return:

<?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>