XSLT 2.0 - 使用值和属性进行过滤

时间:2017-05-22 14:03:20

标签: xml xslt xslt-2.0

在XSLT 2.0版本中寻求帮助。

这是XML:

parse

这是XSL:

<?xml version='1.0' encoding='UTF-8'?>
<peci:Workers_Effective_Stack xmlns:peci="urn:com.workday/peci">
    <peci:Summary>
        <peci:Version>1</peci:Version>
    </peci:Summary>
    <peci:Worker>
        <peci:Worker_Summary>
        </peci:Worker_Summary>
        <peci:Effective_Change peci:Sequence="0">
            <peci:Derived_Event_Code>LOA-C</peci:Derived_Event_Code>
            <peci:Effective_Moment>2017-03-15T00:00:00.000-07:00</peci:Effective_Moment>
            <peci:Entry_Moment>2017-03-14T05:42:16.106-07:00</peci:Entry_Moment>
            <peci:Leave_of_Absence peci:isDeleted="1">
                <peci:Leave_Start_Date>2017-03-15-07:00</peci:Leave_Start_Date>
                <peci:Estimated_Leave_End_Date>2017-04-10-07:00</peci:Estimated_Leave_End_Date>
                <peci:Leave_Last_Day_of_Work>2017-03-14-07:00</peci:Leave_Last_Day_of_Work>
                <peci:Leave_of_Absence_Type>X_01</peci:Leave_of_Absence_Type>
            </peci:Leave_of_Absence>
            <peci:Leave_of_Absence peci:isAdded="1">
                <peci:Leave_Start_Date>2017-03-15-07:00</peci:Leave_Start_Date>
                <peci:Estimated_Leave_End_Date>2017-04-10-07:00</peci:Estimated_Leave_End_Date>
                <peci:Leave_Last_Day_of_Work>2017-03-14-07:00</peci:Leave_Last_Day_of_Work>
                <peci:Leave_of_Absence_Type>X_01</peci:Leave_of_Absence_Type>
            </peci:Leave_of_Absence>
        </peci:Effective_Change>
        <peci:Effective_Change peci:Sequence="1">
            <peci:Derived_Event_Code>RFL</peci:Derived_Event_Code>
            <peci:Effective_Moment>2017-04-13T00:00:00.000-07:00</peci:Effective_Moment>
            <peci:Entry_Moment>2017-03-14T05:42:16.106-07:00</peci:Entry_Moment>
            <peci:Leave_of_Absence peci:isUpdated="1">
                <peci:Leave_Start_Date>2017-03-15-07:00</peci:Leave_Start_Date>
                <peci:Leave_End_Date peci:isAdded="1">2017-04-12-07:00</peci:Leave_End_Date>
                <peci:Estimated_Leave_End_Date>2017-04-10-07:00</peci:Estimated_Leave_End_Date>
                <peci:First_Day_of_Work peci:isAdded="1">2017-04-13-07:00</peci:First_Day_of_Work>
                <peci:Leave_Last_Day_of_Work>2017-03-14-07:00</peci:Leave_Last_Day_of_Work>
                <peci:Leave_of_Absence_Type>X_01</peci:Leave_of_Absence_Type>
            </peci:Leave_of_Absence>
        </peci:Effective_Change>
    </peci:Worker>
</peci:Workers_Effective_Stack>

输出:

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:this="urn:this-stylesheet" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:peci="urn:com.workday/peci"
        >

    <xsl:template match="/">
        <File>
            <xsl:apply-templates>
            </xsl:apply-templates>
        </File>
    </xsl:template>

    <xsl:template match="peci:Effective_Change">        
        <xsl:if test="peci:Leave_of_Absence">
            <xsl:call-template name="TESTID" /> 
        </xsl:if>           
    </xsl:template>

    <xsl:template name="TESTID">        
        <xsl:apply-templates select="peci:Leave_of_Absence">        
        </xsl:apply-templates>
    </xsl:template> 

    <xsl:template match="peci:Leave_of_Absence">    
        <Record>
            <Identification>
                <xsl:text>TEST</xsl:text>
            </Identification>
            <Employee>
                <xsl:value-of select="../peci:Worker_Summary/peci:Employee_ID" />
            </Employee> 
            <Leave_Start_Date>
                <xsl:value-of select="format-date(peci:Leave_Start_Date, '[D01][M01][Y0001]')" />
            </Leave_Start_Date>
            <Leave_End_Date>
                <xsl:value-of select="if (exists(peci:Leave_End_Date)) then format-date(peci:Leave_End_Date, '[D01][M01][Y0001]') else format-date(peci:Estimated_Leave_End_Date, '[D01][M01][Y0001]')" />
            </Leave_End_Date>
            <xsl:choose>
                <xsl:when test="contains(peci:Leave_of_Absence_Type,'_')">
                    <Absence_Code>
                        <xsl:value-of select="substring-after(peci:Leave_of_Absence_Type,'_')" />
                    </Absence_Code>                     
                </xsl:when>
                <xsl:otherwise>             
                    <Absence_Code>
                        <xsl:value-of select="peci:Leave_of_Absence_Type" />
                    </Absence_Code>                     
                </xsl:otherwise>
            </xsl:choose>           
            <Duration>
                <xsl:value-of select="if (exists(peci:Leave_End_Date)) then days-from-duration(xs:date(peci:Leave_End_Date)-xs:date(peci:Leave_Start_Date))+1 else days-from-duration(xs:date(peci:Estimated_Leave_End_Date)-xs:date(peci:Leave_Start_Date))+1" />
            </Duration>             
            <xsl:choose>
                <xsl:when test="./@peci:isAdded or ./@peci:isUpdated">
                    <Delete_Selection>
                    </Delete_Selection>  
                </xsl:when>
                <xsl:otherwise>
                    <Delete_Selection>
                        <xsl:text>9</xsl:text>
                    </Delete_Selection>  
                </xsl:otherwise>
            </xsl:choose>           
        </Record>
    </xsl:template>     
</xsl:stylesheet>

我希望在带有过滤器的xsl中使用当前模板结构获得以下输出。可能吗?当peci:Derived_Event_Code是LOA-C时,过滤我正在寻找的东西,然后忽略相应的peci:Leave_of_Absence,其属性为peci:isAdded。我应该在当前的xsl模板结构中何处引入该过滤器。

期待输出:

<?xml version="1.0" encoding="UTF-8"?>
<File>
    <Record>
        <Identification>TEST</Identification>
        <Employee/>
        <Leave_Start_Date>15032017</Leave_Start_Date>
        <Leave_End_Date>10042017</Leave_End_Date>
        <Absence_Code>01</Absence_Code>
        <Duration>27</Duration>
        <Delete_Selection>9</Delete_Selection>
    </Record>
    <Record>
        <Identification>TEST</Identification>
        <Employee/>
        <Leave_Start_Date>15032017</Leave_Start_Date>
        <Leave_End_Date>10042017</Leave_End_Date>
        <Absence_Code>01</Absence_Code>
        <Duration>27</Duration>
        <Delete_Selection/>
    </Record>
    <Record>
        <Identification>TEST</Identification>
        <Employee/>
        <Leave_Start_Date>15032017</Leave_Start_Date>
        <Leave_End_Date>12042017</Leave_End_Date>
        <Absence_Code>01</Absence_Code>
        <Duration>29</Duration>
        <Delete_Selection/>
    </Record>
</File>

1 个答案:

答案 0 :(得分:0)

值得补充的是xpath-default-namespace="urn:com.workday/peci"stylesheet标记中。 它允许简化您的XSLT,因为您不需要 将peci:放在XPath中(属性除外)。

如果你需要过滤 out 一些元素,那么一个有趣的 另一种方法是写一个空的( do notning )模板 对于这些元素,例如:

<xsl:template match="Leave_of_Absence[@peci:isAdded and ../Derived_Event_Code = 'LOA-C']"/>

在此示例中,您要忽略Leave_of_Absence个元素 其中包含isAdded属性和Derived_Event_Code 具有LOA-C值的邻居,就像您指定的那样。

顺便说一句:为Summary元素添加一个空模板。

<xsl:template match="Summary"/>

否则其内容(1)会显示在第一个Record之前。