Search code examples
xmlxsltxpath-2.0

come out from another document(), xslt


I would like to get current element name but it showing name of another document name. how to get current document element, instead of another document element name?, this is example coding only, don't give the suggestion "get name" out of for-each. Especially i like to get inside of for each loop

<xsl:template match="*:LegisRefLink">
    <xsl:element name="LegiRefered" namespace="http://www.w3.org/1999/xhtml">
      <xsl:variable name="legs" select="document(@href)//*"/>
      <xsl:for-each select="$legs//*:ref">
        <xsl:value-of select="name(.)"/>// I would like to get current document element name, but it's showing $legis/ref. how to get current element instead of another document element name.
        <xsl:value-of select="@refNo"/>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

Please help me to achieve this! Thanks in Advances!

Regards, Saran


Solution

  • Often in XSL, there is a conflict between the current and former context. These usually have to be resolved with variables to preserve the context of a former state:

    <xsl:template match="*:LegisRefLink">
        <xsl:element name="LegiRefered" namespace="http://www.w3.org/1999/xhtml">
          <!-- Could save "." and get the name within the loop but if that's all we want, keep it efficient. -->
          <xsl:variable name="refName" select="name(.)"/>
          <xsl:variable name="legs" select="document(@href)//*"/>
          <xsl:for-each select="$legs//*:ref">
            <xsl:value-of select="$refName"/>     <!-- Use the value saved earlier when we did have the correct context. -->
            <xsl:value-of select="@refNo"/>
          </xsl:for-each>
        </xsl:element>
      </xsl:template>
    

    Notice that it is using the same syntax as the document you are loading - a variable (which XSL won't allow you to change) is just a way of saving a particular node, result or value.