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

时间:2013-04-30 11:03:29

标签: xslt xpath xslt-1.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进行哪些更改。?

1 个答案:

答案 0 :(得分: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>