如何创建属性节点并将其附加到输出节点..



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

输入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进行哪些更改?

你不能给元素以外的东西添加属性。这是不尝试手动构造开始和结束标记的众多原因之一。试试这个:

<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>

相关内容

  • 没有找到相关文章

最新更新