我正在考虑将源输入 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>
需要对代码进行哪些更正才能使其正常工作..?
编辑:纠正了我代码中的所有愚蠢错误。但就输出而言仍然没有运气。