【发布时间】:2013-04-02 07:24:13
【问题描述】:
我正在考虑将源输入 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>
....
以下是我目前正在工作的代码的 sn-p。由于某种原因,这不起作用。
<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>
需要对代码进行哪些更正才能使其正常工作..?
编辑:纠正了我代码中的所有愚蠢错误。但就输出而言仍然没有运气。
【问题讨论】:
-
为什么您的 xml 输入的结束标记中有反斜杠?
<\record>, <\teacher>等等……应该是</record>, </teacher> -
还有几个错误:
<xsl:apply-template>必须是<xsl:apply-templates>(注意复数形式!),<xsl:apply-template select = "teacher">元素应该关闭(<xsl:apply-templates select = "teacher" />),不应该@ 987654333@实际上是<xsl:apply-templates select = "Student" />?我认为 XSLT 区分大小写... -
@Miklos Aubert,正如你所指出的,我已经对代码进行了更改。仍然无法弄清楚还缺少什么。
标签: xslt-1.0