XML : How do I get the node name of parameter in XSLT template?

I'm trying to update some XSLT to accomodate a new XML format. The new structure pretty much breaks my former template logic, but I'm trying to keep from having to rewrite the whole thing over.

Previous XML

  <root>      <Node1>Foo</Node1>      <Node2>Bar</Node2>      <Node3>Bat</Node3>      <Anothers>          <Node1>Related Foo</Node1>          <Node2>Related Bar</Node2>          <Node3>Related Bat</Node3>      </Anothers>  </root>    

New XML

  <root>      <Node1>Foo</Node1>      <Node2>Bar</Node2>      <Node3>Bat</Node3>      <AnotherNode1>Related Foo</Node1>      <AnotherNode2>Related Bar</Node2>      <AnotherNode3>Related Bat</Node3>  </root>    

As you can see, previously the "AnotherNodes" were children under <Anothers/>. Now they are listed as siblings.

My XSLT was accounting for the previous structure and the names being the same, just at different levels.

  <xsl:template name="valueOrParentOrNone">      <xsl:param name="fieldValue"/>      <xsl:choose>          <xsl:when test="$fieldValue">              <xsl:value-of select="concat($quote, functx:stripCommas($fieldValue), $quote)" />          </xsl:when>          <xsl:when test="../$fieldValue">              <xsl:value-of select="concat($quote, functx:stripCommas(../$fieldValue), $quote)" />          </xsl:when>          <xsl:otherwise>              <xsl:text>"None"</xsl:text>          </xsl:otherwise>      </xsl:choose>  </xsl:template>    

I'm calling the above template all over the place like so:

  <xsl:template match="Anothers">      <xsl:call-template name="valueOrParentOrNone">          <xsl:with-param name="fieldValue" select="Node1"/>      </xsl:call-template>      <!-- delimiter of some sort -->      <xsl:call-template name="valueOrParentOrNone">          <xsl:with-param name="fieldValue" select="Node2"/>      </xsl:call-template>      <!-- delimiter of some sort -->              <xsl:call-template name="valueOrParentOrNone">          <xsl:with-param name="fieldValue" select="Node3"/>      </xsl:call-template>  </xsl:template>    

What I'd like to do is grab the node name from the parameter of the valueOrParentOrNone template, remove the "Another" from it, and use that as the value for the secondary option in my <choose/> so it will look something like:

  <xsl:template name="valueOrParentOrNone">      <xsl:param name="fieldValue"/>      <xsl:variable name="fieldName" select="replace($fieldValue, 'Another', '')"/>      <xsl:choose>          <xsl:when test="$fieldValue">              <xsl:value-of select="concat($quote, functx:stripCommas($fieldValue), $quote)" />          </xsl:when>          <xsl:when test="../$fieldName">              <xsl:value-of select="concat($quote, functx:stripCommas(../$fieldName), $quote)" />          </xsl:when>          <xsl:otherwise>              <xsl:text>"None"</xsl:text>          </xsl:otherwise>      </xsl:choose>  </xsl:template>    

Is this possible to reuse this template like this? Or do I need to rewrite the whole XSLT using a completely different approach?

No comments:

Post a Comment