0

個々のレコードがそれであるか、<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>

それが機能するには、コードにどのような修正が必要ですか..?

編集:私のコードで行われたすべてのばかげた間違いを修正しました.しかし、出力に関する限り、まだ運がありません.

4

1 に答える 1

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>

Basically, other than the errors I had told you about in my comments, you forgot to wrap the <xsl:when> and <xsl:otherwise> of your for-each loop inside an <xsl:choose> element.

于 2013-04-02T10:22:46.383 に答える