Xslt Concat与Xsl

Xslt Concat与Xsl,xslt,sharepoint-2010,web-parts,Xslt,Sharepoint 2010,Web Parts,让我首先说我是Xsl的一个完全的noob。我试图完成我觉得很简单的事情,但是我很难理解语法 我有一个获取位置名称的变量(例如,Chicago或Orland) 然后我有一个变量,它获取一个没有区号的电话号码 所以我想做的是一个If语句,Concat(“区号”和“电话号码”)基于If位置是芝加哥(773)或奥尔兰(708) 变量: xsl:variable name="haswph" select="string-length(workphone) > 0" xsl:var

让我首先说我是Xsl的一个完全的noob。我试图完成我觉得很简单的事情,但是我很难理解语法

我有一个获取位置名称的变量(例如,Chicago或Orland)

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

所以我想做的是一个If语句,Concat(“区号”和“电话号码”)基于If位置是芝加哥(773)或奥尔兰(708)

变量:

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

  • 如有任何建议或正确方向的观点,将不胜感激

    谢谢, 布兰登

    尼格罗,布兰登L。 信息技术 555-5555 约翰学术中心
    如果我正确理解了这个问题,解决方案只需将此模板添加到当前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>
    
    
    (773) 
    (708) 
    
    将此“$office”变量替换为您在原始帖子中引用的变量

    编辑:这里有一个完整的转换,其中前一个模板应用于示例输入XML(顺便说一句:尝试遵循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>
    
    
    尼格罗,布兰登L。
    信息技术
    555-5555
    约翰学术中心
    
    XSL:

    
    员工财产

    (773) (708)
    输出:

      <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>
    
    <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>
    
    
    员工1物业

      尼格罗,布兰登L 信息技术
    • (773)555-5555
    • 约翰学术中心
    但是,我不鼓励您使用这种“变量+如果”方法,并尝试使用干净的xsl pushy样式(这会得到完全相同的结果):

    
    员工财产

    (773) (708)

  • 请编辑您的帖子,以包含输入XML和所需输出的示例。此外,输入中的“位置”在哪里?你还没有展示是什么决定了办公室是芝加哥还是洛杉矶。这个xsl来自于一个SharePoint Web部件。很好,但我们仍然需要查看输入和输出的示例,以及XML中的“位置”来自何处。请原谅我的无能。我相信输入已经从AD设置好了,就在Web部件加载时。在代码的顶部,WorkPhone已经设置了我的号码,officenumber也设置了我的号码。我在我的原始帖子中添加了XML代码。它不起作用:(在我将其添加到代码中后,没有任何更改。模板应该与park equal workphone匹配,还是与OfficeField中的workphone id匹配(在第一篇文章中说明)。我应该把它放在工作电话输出的下方还是上方?好的,对不起。也许我给你的信息太少了。现在我将编辑这篇文章,给你所有必要的细节。谢谢耐心。我没有意识到xsl中有HTML。所以我想测试一下功能,它是否像复制到记事本一样简单,设置变量,然后在浏览器中打开?是的,应该是这样。您可以查看另一个答案,该答案解释了如何设置它:
    <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>