0

在以下场景中,我无法找到将属性节点附加到输出节点的方法。

输入xml:

<record>
    <user>
        <field name="LastName">user33</field>
        <field name="FirstName">user33</field>
        <field name="Title"/>
        <field name="Email">user33@gmail.com</field>
        <field name="WorkPhone"/>
        <field name="Fax"/>
        <field name="Description">new user</field>
        <field name="Group Member"> group1</field>
    </user>
</record>

预期输出:

<add class="user" id-val="user33 user33" >          
        <add-value attr="LastName">
            <value type="string">user33</value>
        </add-attr>
        <add-value attr="FirstName">
            <value type="string">user33</value>
        </add-value>
        <add-value attr="Email">
            <value type="string">user33@gamil.com</value>
        </add-value>
        <add-value attr="Description">
            <value type="string">new user</value>
        </add-value>
    </add>

这是我到目前为止的 xslt 片段。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:variable name="lessThan" select="'&#x3C;'"/>
  <xsl:variable name="GreaterThan" select="'&#x3E;'"/>

  <xsl:template match="user">
    <xsl:variable name="temp1" select="concat(field[@name=$srcdn-field-name1],' ')"/>
    <xsl:variable name="temp2" select="concat($temp1,field[@name=$srcdn-field-name2])"/>
    <xsl:variable name="src" select="translate($temp2,'+=,.\','-----')"/>

    <xsl:value-of disable-output-escaping="yes" select="$lessThan"/>
    <xsl:text>add</xsl:text>
    <xsl:value-of disable-output-escaping="yes" select="$GreaterThan"/>

    <!-- it is required to add attribute id-val to element <add> with value of  $src-->

    <xsl:for-each select="field[string()]">
      <xsl:variable name="fieldValue" select="normalize-space(.)"/>
      <xsl:choose>
        <xsl:when test="@name !='Group Member'">
          <add-value attr="{@name}">
            <value type="string">
              <xsl:value-of select="$fieldValue"/>
            </value>
          </add-value>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of disable-output-escaping="yes" select="$lessThan"/>
          <xsl:text>/add</xsl:text>
          <xsl:value-of disable-output-escaping="yes" select="$GreaterThan"/>

          <!--perform some other operations-->

        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

现在我的要求是将 id-val 和 class 作为<add>.. 的属性。在这种情况下,<xsl:attribute>它不起作用。我需要对我的 xslt 进行哪些更改。?

4

1 回答 1

0

您不能将属性添加到不是元素的东西。这是不尝试手动构建开始和结束标签的众多原因之一。试试这个:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="user">
    <xsl:variable name="temp1" select="concat(field[@name=$srcdn-field-name1],' ')"/>
    <xsl:variable name="temp2" select="concat($temp1,field[@name=$srcdn-field-name2])"/>
    <xsl:variable name="src" select="translate($temp2,'+=,.\','-----')"/>

    <add class="user" id-val="{$src}">

      <!-- it is required to add attribute id-val to element <add> with value of  $src-->

      <xsl:for-each select="field[string()]
                              [not((. | preceding-sibling::field)
                                             [@name = 'Group Member'])]">
        <add-value attr="{@name}">
          <value type="string">
            <xsl:value-of select="normalize-space()"/>
          </value>
        </add-value>
      </xsl:for-each>

    </add>
  </xsl:template>
</xsl:stylesheet>
于 2013-04-30T11:51:58.637 回答