【问题标题】:Transforming source xml to another xml using xslt使用 xslt 将源 xml 转换为另一个 xml
【发布时间】: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 输入的结束标记中有反斜杠? &lt;\record&gt;, &lt;\teacher&gt; 等等……应该是 &lt;/record&gt;, &lt;/teacher&gt;
  • 还有几个错误:&lt;xsl:apply-template&gt; 必须是&lt;xsl:apply-templates&gt;(注意复数形式!),&lt;xsl:apply-template select = "teacher"&gt; 元素应该关闭(&lt;xsl:apply-templates select = "teacher" /&gt;),不应该@ 987654333@实际上是&lt;xsl:apply-templates select = "Student" /&gt;?我认为 XSLT 区分大小写...
  • @Miklos Aubert,正如你所指出的,我已经对代码进行了更改。仍然无法弄清楚还缺少什么。

标签: xslt-1.0


【解决方案1】:

好的,我终于有时间使用 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>

基本上,除了我在 cmets 中告诉您的错误之外,您忘记将 for-each 循环的 &lt;xsl:when&gt;&lt;xsl:otherwise&gt; 包装在 &lt;xsl:choose&gt; 元素中。

【讨论】:

  • ,感谢您抽出宝贵时间回复。它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多