我正在考虑将源输入xml转换为输出xml格式,具体取决于单个记录是<Student>
还是<teacher>
输入xml :
<staff>
<record>
<Student>
<field name="LastName">Dtext</field>
<field name="FirstName"></field>
<field name="Class">5</field>
<field name="Email">Dtext-user33@nova.com</field>
</Student>
</record>
<record>
<Student>
....
</Student>
</record>
<record>
<Teacher>
<field name="LastName">Dtext-user35</field>
<field name="FirstName"></field>
<field name="Email">Dtext-user35@nova.com</field>
<field name="Experience">10</field>
<field name="Qualification"></field>
</Teacher>
</record>
....
....
输出xml:
<input>
<add user="Student" >
<add-value value-name="LastName">
<value type="string">Dtext</value>
</add-value>
<add-value value-name="FirstName">
<value type="string"></value>
</add-value>
<add-value value-name="class">
<value type="string">5</value>
</add-value>
<add-value value-name="Email">
<value type="string">Dtext-user33@novell.com</value>
</add-value>
</add>
或
<input>
<add user="Teacher" >
<add-value value-name="LastName">
<value type="string">Dtext-user35</value>
</add-value>
....
下面是我正在使用的代码片段。由于某种原因,这不起作用。
<xsl:template match="/">
<xsl:choose>
<xsl:when test="staff">
<xsl:for-each select="staff/record">
<xsl:when test="name(./*[1])= 'Student'">
<xsl:apply-template select = "Student"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-template select = "Teacher">
</xsl:otherwise>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Student">
<input>
<add user="Student">
<xsl:for-each select="field[string()]">
<xsl:variable name="fieldValue" select="normalize-space(.)"/>
<add-value value-name="{@name}">
<value type="string">
<xsl:value-of select="$fieldValue"/>
</value>
</add-value>
</xsl:for-each>
</add>
</input>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="Teacher">
<input>
<add user="Teacher">
<xsl:for-each select="field[string()]">
<xsl:variable name="fieldValue" select="normalize-space(.)"/>
<add-value value-name="{@name}">
<value type="string">
<xsl:value-of select="$fieldValue"/>
</value>
</add-value>
</xsl:for-each>
</add>
</input>
</xsl:template>
</xsl:stylesheet>
它需要对代码进行哪些更正...?
编辑:纠正了我的代码中出现的所有愚蠢错误。但就输出而言,仍然没有运气。
答案 0 :(得分:0)
好的,我终于花了一些时间来使用Visual Studio调试XSLT(如果可以,我强烈建议您使用XSLT调试器)
以下是您的XSLT文件的外观:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:choose>
<xsl:when test="staff">
<xsl:for-each select="staff/record">
<xsl:choose>
<xsl:when test="name(./*[1])= 'Student'">
<xsl:apply-templates select = "Student" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select = "Teacher" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Student">
<input>
<add user="Student">
<xsl:for-each select="field[string()]">
<xsl:variable name="fieldValue" select="normalize-space(.)"/>
<add-value value-name="{@name}">
<value type="string">
<xsl:value-of select="$fieldValue"/>
</value>
</add-value>
</xsl:for-each>
</add>
</input>
</xsl:template>
<xsl:template match="Teacher">
<input>
<add user="Teacher">
<xsl:for-each select="field[string()]">
<xsl:variable name="fieldValue" select="normalize-space(.)"/>
<add-value value-name="{@name}">
<value type="string">
<xsl:value-of select="$fieldValue"/>
</value>
</add-value>
</xsl:for-each>
</add>
</input>
</xsl:template>
</xsl:stylesheet>
基本上,除了我在评论中告诉你的错误之外,你忘了将<xsl:when>
循环的<xsl:otherwise>
和for-each
包裹在<xsl:choose>
元素中