仅当第n个重复子级中的任何一个包含比今天晚的日期时,才复制重复节点

时间:2018-08-31 13:51:32

标签: xslt xslt-1.0

我想过滤传入的xml文档,以仅将包含日期晚于今天的那些记录保留在某些重复的第n个子节点上。 我试图将身份模板与模板一起使用,以仅匹配那些具有谓词的记录,但是如果可能的话,我似乎无法获得正确的谓词。

示例输入:

<?xml version="1.0" encoding="utf-8"?>
<Example>
    <Header>
        <ElementA/>
        <ElementB/>
    </Header>
    <Records>   
        <Record>    
            <Person>    
                <ElementC>1</ElementC>
            </Person>
            <Employers> 
                <Employer>  
                    <Identification>
                        <ElementD/>
                        <ElementE/>
                    </Identification>
                </Employer>
            </Employers>
        </Record>
        <Record>    
            <Person>    
                <ElementC>2</ElementC>
            </Person>
            <Employers> 
                <Employer>  
                    <Identification>
                        <ElementD/>
                        <ElementE/>
                    </Identification>
                    <History> 
                        <HistoryRecord>
                            <Period>
                                <Date>2017-08-01</Date>
                            </Period>
                        </HistoryRecord>
                        <HistoryRecord>
                            <Period>
                                <Date>2017-08-01</Date>
                            </Period>
                            <Period>
                                <Date>2018-10-01</Date>
                            </Period>
                        </HistoryRecord>
                    </History>
                </Employer>
            </Employers>
        </Record>
        <Record>    
            <Person>    
                <ElementC>3</ElementC>
            </Person>
            <Employers> 
                <Employer>  
                    <Identification>
                        <ElementD/>
                        <ElementE/>
                    </Identification>
                    <History> 
                        <HistoryRecord>
                            <Period>
                                <Date>2017-11-01</Date>
                            </Period>
                        </HistoryRecord>
                    </History>
                </Employer>
            </Employers>
        </Record>
        <Record>    
            <Person>    
                <ElementC>4</ElementC>
            </Person>
            <Employers> 
                <Employer>  
                    <Identification>
                        <ElementD/>
                        <ElementE/>
                    </Identification>
                    <History> 
                        <HistoryRecord>
                            <Period>
                                <Date>2018-11-01</Date>
                            </Period>
                        </HistoryRecord>
                    </History>
                </Employer>
            </Employers>
        </Record>
    </Records>
</Example>

想要的输出:

 <?xml version="1.0" encoding="utf-8"?>
<Example>
    <Header>
        <ElementA/>
        <ElementB/>
    </Header>
    <Records>   
            <Person>    
                <ElementC>2</ElementC>
            </Person>
            <Employers> 
                <Employer>  
                    <Identification>
                        <ElementD/>
                        <ElementE/>
                    </Identification>
                    <History> 
                        <HistoryRecord>
                            <Period>
                                <Date>2017-08-01</Date>
                            </Period>
                        </HistoryRecord>
                        <HistoryRecord>
                            <Period>
                                <Date>2017-08-01</Date>
                            </Period>
                            <Period>
                                <Date>2018-10-01</Date>
                            </Period>
                        </HistoryRecord>
                    </History>
                </Employer>
            </Employers>
        </Record>
        <Record>    
            <Person>    
                <ElementC>4</ElementC>
            </Person>
            <Employers> 
                <Employer>  
                    <Identification>
                        <ElementD/>
                        <ElementE/>
                    </Identification>
                    <History> 
                        <HistoryRecord>
                            <Period>
                                <Date>2018-11-01</Date>
                            </Period>
                        </HistoryRecord>
                    </History>
                </Employer>
            </Employers>
        </Record>
    </Records>
</Example>

1 个答案:

答案 0 :(得分:0)

<!-- Get today's date somehow -->
<xsl:variable name="todaysDate" select="'2018-09-06'"/>

<!--  You need the identity template -->
<xsl:template match="node()|@*">
   <xsl:apply-templates select="node()|@*"/>
</xsl:template>

<xsl:template match="Record">
  <!-- Since your date is in the yyyy-mm-dd format, you can do a text compare.  -->
  <xsl:if test=".//Date[. &gt; $todaysDate][1]">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:if>
</xsl:template>