您好我是XSLT新手我想要转换学生名称映射的Xml数据集,其中有一个学生名称和卷号的数据集。 classwise。
我想使用xslt,但我不想将所有标签添加到要映射的列名标签中,并按原样放置其他列。
这是输入和输出数据集示例。 xml是来自c#代码的普通数据集.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>
由于 阿曼
答案 0 :(得分: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>
以下样式表:
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>