XSLT转换取决于父项值

时间:2017-08-14 12:49:21

标签: xml xslt transformation

我正努力跟进工作。

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看起来像这样:

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

如果值为“0x00000000”,我需要检查“ErrorCode”,并在该条件下单独处理行。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

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