与Xsl的Concat

时间:2012-11-05 18:37:30

标签: xslt sharepoint-2010 web-parts

首先让我说我是Xsl的完整菜鸟。我想要完成我觉得非常简单的事情,但我很难理解语法。

我有一个获取位置名称的变量(例如芝加哥或奥兰德)

然后我有一个变量来获取没有区号的电话号码。

所以我想要做的是如果位置是芝加哥(773)或奥兰德(708)的话,Concat(“区号”带“电话号码”)的If声明。

变量:

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

输出:

  <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>

任何建议或正确方向的观点都将不胜感激。

谢谢, 布兰登

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

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,解决方案只需将此模板添加到当前的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>

将此“$ office”变量替换为您在原始帖子中引用的变量。

编辑:这里有一个完整的转换,其中前一个模板应用于示例输入XML(BTW:尝试遵循Jim Garrison建议。发布完整的输入/输出XML而不是其中的部分内容未来的帖子,你会在第一次尝试时得到确切的回复。)

输入:

    <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>

输出:

<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>

但是,我不鼓励你使用这种“变量+ if”方法,并尝试使用干净的xsl pushy样式(这实现了完全相同的结果):

<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>