Search code examples
xsltsharepoint-2010web-parts

Concat with Xsl


Let me start off by saying I am a complete noob to Xsl. I am trying to accomplish what I feel is pretty simple, but I am having a hard time understanding the syntax.

I have a variable that gets a location name (ex. Chicago or Orland)

Then I have a variable that gets a phone number without an area code.

So what I am trying to do is a If statement that Concat("Area Code" with the "Phone Number") based off if location is Chicago (773), or Orland (708).

Variables:

xsl:variable name="haswph"       select="string-length(workphone) > 0"
xsl:variable name="hasonum"      select="string-length(officenumber) > 0"

Output:

  <xsl:if test="$haswph">
    <li id="PhoneField">
      <xsl:apply-templates select="hithighlightedproperties/workphone" />
    </li>
  </xsl:if>
  <xsl:if test="$hasonum">
    <li id="OfficeField">
      <xsl:apply-templates select="hithighlightedproperties/officenumber" />
    </li>
  </xsl:if>

Any suggestions, or a point in the right direction would be greatly appreciated.

Thanks, Brandon

<preferredname>Nigro, Brandon L.</preferredname> 
<yomidisplayname></yomidisplayname>
<department>Information Technology</department> 
<workphone>555-5555</workphone>
<officenumber>John Academic Center</officenumber>

Solution

  • If I understand the question correctly, solution passes through simply adding this template to your current xsl:

    <xsl:template match="workphone">
        <xsl:if test="$office='Chicago'">(773) </xsl:if>
        <xsl:if test="$office='Orland'">(708) </xsl:if>
        <xsl:value-of select="."/>
    </xsl:template>
    

    replacing this "$office" variable by the one you referred in your original post.

    EDIT: Here you have a a complete tranformation where the previous template is applied to a sample input XML (BTW: Try following Jim Garrison suggestion. Post complete input/output XML instead of parts of them in future posts and you'll get the exact response at the first try).

    Input:

        <Office code="Chicago">
            <Employee id="1">
                <hithighlightedproperties>
                    <preferredname>Nigro, Brandon L.</preferredname>
                    <yomidisplayname></yomidisplayname>
                    <department>Information Technology</department>
                    <workphone>555-5555</workphone>
                    <officenumber>John Academic Center</officenumber>
                </hithighlightedproperties>
            </Employee>
        </Office>
    

    XSL:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" indent="yes"/>
        <xsl:variable name="office" select="Office/@code"/>
        <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
        <xsl:template match="Office">
            <html>
                <body>
                    <xsl:apply-templates select="Employee"/>
                </body>
            </html>
        </xsl:template>
        <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
        <xsl:template match="Employee">
            <xsl:variable name="haswph" select="string-length(hithighlightedproperties/workphone) &gt; 0"/>
            <xsl:variable name="hasonum" select="string-length(hithighlightedproperties/officenumber)&gt; 0"/>
            <p>Employee <xsl:value-of select="@id"/> Properties</p>
            <ul>
                <li id="Name"><xsl:value-of select="hithighlightedproperties/preferredname"/></li>
                <xsl:if test="string-length(hithighlightedproperties/yomidisplayname)">
                    <li id="DisplayNameField">
                        <xsl:apply-templates select="hithighlightedproperties/yomidisplayname"/>
                    </li>
                </xsl:if>
                <li id="DepartmentField"><xsl:value-of select="hithighlightedproperties/department"/></li>
                <xsl:if test="$haswph">
                    <li id="PhoneField">
                        <xsl:apply-templates select="hithighlightedproperties/workphone"/>
                    </li>
                </xsl:if>
                <xsl:if test="$hasonum">
                    <li id="OfficeField">
                        <xsl:apply-templates select="hithighlightedproperties/officenumber"/>
                    </li>
                </xsl:if>
            </ul>
        </xsl:template>
        <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
        <xsl:template match="workphone">
            <xsl:if test="$office='Chicago'">(773) </xsl:if>
            <xsl:if test="$office='Orland'">(708) </xsl:if>
            <xsl:value-of select="."/>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <html>
        <body>
            <p>Employee 1 Properties</p>
            <ul>
                <li id="NameField">Nigro, Brandon L.</li>
                <li id="DepartmentField">Information Technology</li>
                <li id="PhoneField">(773) 555-5555</li>
                <li id="OfficeField">John Academic Center</li>
            </ul>
        </body>
    </html>
    

    However, I discourage you from using this "variable + if" approach, and try using the clean xsl pushy style (This achieves exactly the same result):

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" indent="yes"/>
        <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
        <xsl:template match="Office">
            <html>
                <body>
                    <xsl:apply-templates select="Employee"/>
                </body>
            </html>
        </xsl:template>
        <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
        <xsl:template match="Employee">
            <p>Employee <xsl:value-of select="@id"/> Properties</p>
            <ul>
                <xsl:apply-templates select="hithighlightedproperties/preferredname" mode="li">
                    <xsl:with-param name="id" select="'NameField'"/>
                </xsl:apply-templates>
                <xsl:apply-templates select="hithighlightedproperties/yomidisplayname" mode="li">
                    <xsl:with-param name="id" select="'DisplayNameField'"/>
                </xsl:apply-templates>
                <xsl:apply-templates select="hithighlightedproperties/department" mode="li">
                    <xsl:with-param name="id" select="'DepartmentField'"/>
                </xsl:apply-templates>
                <xsl:apply-templates select="hithighlightedproperties/workphone" mode="li">
                    <xsl:with-param name="id" select="'PhoneField'"/>
                </xsl:apply-templates>
                <xsl:apply-templates select="hithighlightedproperties/officenumber" mode="li">
                    <xsl:with-param name="id" select="'OfficeField'"/>
                </xsl:apply-templates>
            </ul>
        </xsl:template>
        <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
        <xsl:template match="workphone">
            <xsl:variable name="office" select="../../../@code"/>
            <xsl:if test="$office='Chicago'">(773) </xsl:if>
            <xsl:if test="$office='Orland'">(708) </xsl:if>
            <xsl:value-of select="."/>
        </xsl:template>
        <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
        <xsl:template match="*[string-length(.)&gt;0]" mode="li">
            <xsl:param name="id" select="local-name()"/>
            <li id="{$id}">
                <xsl:apply-templates select="."/>
            </li>
        </xsl:template>
    </xsl:stylesheet>