代码之家  ›  专栏  ›  技术社区  ›  Alex

XSLT转换取决于父项值

  •  2
  • Alex  · 技术社区  · 9 年前

    我正在努力让下面的人工作。

    XML:

    <Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <Result ID="4,New">
        <ErrorCode>0x00000000</ErrorCode>
        <ID />
        <z:row ows_ContentTypeId="0x0100142A0297E606694EA802A784F2232A63" ows_Title="APT.2342362" ows_LinkTitleNoMenu="APT.2342362" ows_LinkTitle="APT.2342362" ows_LinkTitle2="APT.2342362" ows_FacilityNumber="2342362" ows_Status="New" ows_TenantArea="17;#Africa &amp; Asia Pacific" xmlns:z="#RowsetSchema" />
    </Result>
    <Result ID="5,New">
        <ErrorCode>0x00000000</ErrorCode>
        <ID />
        <z:row ows_ContentTypeId="0x0100142A0297E606694EA802A784F2232A63" ows_Title="APT.2342342" ows_LinkTitleNoMenu="APT.2342342" ows_LinkTitle="APT.2342342" ows_LinkTitle2="APT.2342342" ows_FacilityNumber="2342342" ows_Status="New" ows_TenantArea="17;#Africa &amp; Asia Pacific" xmlns:z="#RowsetSchema" />
    </Result>
    

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output indent="yes" method="xml"/>
        <xsl:template match="/" name="ShowVariables">
            <xsl:for-each select="//*[name()='z:row']">
                <xsl:value-of select="@ows_Title"/>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    

    2 回复  |  直到 9 年前
        1
  •  2
  •   Björn Tantau    9 年前

    使用父选择器 .. 检查父项。请记住向样式表中添加正确的命名空间。

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:soap="http://schemas.microsoft.com/sharepoint/soap/">
        <xsl:output indent="yes" method="xml"/>
        <xsl:template match="/" name="ShowVariables">
            <xsl:for-each select="//*[name()='z:row']">
                <xsl:choose>
                    <xsl:when test="../soap:ErrorCode = '0x00000000'">
                        ErrorCode reached
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="@ows_Title"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    
        2
  •  1
  •   Laurent Maillet    9 年前

    另一种对ErrorCode设置条件的方法是创建单独的模板。

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:z="#RowsetSchema" xmlns:soap="http://schemas.microsoft.com/sharepoint/soap/" version="1.0">
        <xsl:output indent="yes" method="xml"/>
        <xsl:template match="/">
            <xsl:apply-templates select="//soap:Result"/>
        </xsl:template>
        <xsl:template match="soap:Result[soap:ErrorCode = '0x00000000']">
            Error detected
        </xsl:template>
        <xsl:template match="soap:Result[soap:ErrorCode != '0x00000000']">
            <xsl:value-of select="z:row/@ows_Title"/>
        </xsl:template>
    </xsl:stylesheet>