Trying to read xml element value in xsl

31,931

Your ?????????????????? can be <xsl:value-of select="."/> (i.e. the string-value of the context element.) It has no connection to $elementName.

You can do this much more succinctly like this:

<xsl:for-each select="*">
  <xsl:variable name="elementName" select="name()"/>
  <xsl:variable name="elementValue" select="string(.)"/>
</xsl:for-each>

However your template is really strange. You're collecting these variables but you're not doing anything with them--they won't appear in the output. What output are you trying to get?

Use of for-each is generally a code smell. In almost all cases you're better off with multiple templates:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="no" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Person/*">
    <xsl:variable name="elementName" select="name()"/>
    <xsl:variable name="elementValue" select="string(.)"/>
</xsl:template>

</xsl:stylesheet>

This pattern where you copy almost everything and change just a little bit of the xml is very common and very powerful and you should learn how to use it.

Share:
31,931
user1818397
Author by

user1818397

Updated on April 07, 2020

Comments

  • user1818397
    user1818397 about 4 years

    I am new to XSL. I am trying to read values of XML elements using a XSL file. My XML file is like:

    <PersonList>
      <Person>
        <Name>person1</Name>
        <Age>21</Age>
      </Person>
      <Person>
        <Name>person2</Name>
        <Age>21</Age>
      </Person>
    </PersonList>
    

    My XSL file is like:

    <xsl:stylesheet version="1.0" xmlns=...>
      <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml declaration="no" />
      <xsl template match="/">
        <PersonList>
          <xsl:for-each select="PersonList/Person">
            <Person>
              <xsl:for-each select="*">
                <xsl:variable name="elementName">
                  <xsl:value-of select="name(.)" />
                </xsl:variable>
                <xsl:variable name="elementValue">
                  ???
                </xsl:variable>
              </xsl:for-each>
            </Person>
          </xsl:for-each>
        </PersonList> 
      </xsl:template>
    </xsl:stylesheet>
    

    How should I replace ??? to get the value of the element stored in the elementName variable. I tried the following three lines separately:

    <xsl:value-of select="value(.)" /> 
    <xsl:value-of select="value($elementName)" />
    <xsl:value-of select="$elementName" />
    

    but no luck. Please help!