XSL循环和换行问题

时间:2014-02-07 15:11:58

标签: xml xslt

我有一个工人记录的xml文档我必须通过xml文档循环并选择符合特定条件的记录。对于每个匹配,我需要在输出文件中写一行后跟换行符(除了最后一条记录,因为EOF中的空行将在下游系统中断)。有些记录会符合多个标准......这是我遇到一些困难的地方,请在下面示例代码..并提前感谢您的帮助..

RESULT 史密斯(换行符) 史密斯 - FulltimeSMITH(换行) 史密斯(换行符) 琼斯 - 全职

预期结果
SMITH(换线) 史密斯 - 全职(linebreak) SMITH(换线) SMITH(换线) 琼斯 - 全职

提前感谢任何建议......

XML

<?xml version="1.0"?>
<Worker_Sync>


    <Worker>
        - <Group name="Employee Changes">
            <Field name="LastName">Jones</Field> 
            <Field name="OriginalHireDt">03211977</Field> 
            <Field name="ParticipationDt">05011977</Field>
            <Field name="Fulltime">False</Field>
        </Group>
    </Worker>
    <Worker>
        - <Group name="Employee Changes">
            <Field name="LastName">Smith</Field> 
            <Field name="OriginalHireDt">03211977</Field> 
            <Field name="ParticipationDt">05011977</Field>
            <Field name="Fulltime">False</Field>
        </Group>
    </Worker>
    <Worker>
        - <Group name="Employee Changes">
            <Field name="LastName">Smith</Field> 
            <Field name="OriginalHireDt">03211977</Field> 
            <Field name="ParticipationDt">05011977</Field>
            <Field name="Fulltime">True</Field>
        </Group>
    </Worker>
    <Worker>
        - <Group name="Employee Changes">
            <Field name="LastName">Smith</Field> 
            <Field name="OriginalHireDt">03211977</Field> 
            <Field name="ParticipationDt">05011977</Field>
            <Field name="Fulltime">False</Field>
        </Group>
    </Worker>
    <Worker>
        - <Group name="Employee Changes">
            <Field name="LastName">Jones</Field> 
            <Field name="OriginalHireDt">03211977</Field> 
            <Field name="ParticipationDt">05011977</Field>
            <Field name="Fulltime">True</Field> 
        </Group>
    </Worker>
    <Worker>
        - <Group name="Employee Changes">
            <Field name="LastName">Jones</Field> 
            <Field name="OriginalHireDt">03211977</Field> 
            <Field name="ParticipationDt">05011977</Field>
            <Field name="Fulltime">False</Field>
        </Group>
    </Worker>
</Worker_Sync>

XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="utf-8"/>

    <xsl:template match="/">
        <xsl:for-each select="Worker_Sync/Worker/Group[Field[@name='LastName']='Smith' 
            or 
            Field[@name='Fulltime']='True']">            
            <xsl:if test="Field[@name='Fulltime']='True'">
                <xsl:value-of select="Field[@name='LastName']"/>  <xsl:text> - Fulltime</xsl:text>
            </xsl:if>
            <xsl:if test="Field[@name='LastName']='Smith'">
                <xsl:value-of select="upper-case(Field[@name='LastName'])"/>
            </xsl:if>
            <xsl:if test="position()!=last()">
                <xsl:text>&#10;</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

新XML

<Worker>
    - <Group name="Employee Changes">
        <Field name="LastName">Jones</Field> 
        <Field name="OriginalHireDt">03211977</Field> 
        <Field name="ParticipationDt">05011977</Field>
        <Field name="Fulltime">False</Field>
    </Group>
</Worker>
<Worker>
    - <Group name="Employee Changes">
        <Field name="LastName">Smith</Field> 
        <Field name="OriginalHireDt">03211977</Field> 
        <Field name="ParticipationDt">05011977</Field>
        <Field name="Fulltime">False</Field>
    </Group>
</Worker>
<Worker>
    - <Group name="Employee Changes">
        <Field name="LastName">Smith</Field> 
        <Field name="OriginalHireDt">03211977</Field> 
        <Field name="ParticipationDt">05011977</Field>
        <Field name="Fulltime">True</Field>
    </Group>
</Worker>
<Worker>
    - <Group name="Employee Changes">
        <Field name="LastName">Smith</Field> 
        <Field name="OriginalHireDt">03211977</Field> 
        <Field name="ParticipationDt">05011977</Field>
        <Field name="Fulltime">False</Field>
    </Group>
</Worker>
<Worker>
    - <Group name="Employee Changes">
        <Field name="LastName">Jones</Field> 
        <Field name="OriginalHireDt">03211977</Field> 
        <Field name="ParticipationDt">05011977</Field>
        <Field name="Fulltime">True</Field> 
    </Group>
</Worker>
<Worker>
    - <Group name="Employee Changes">
        <Field name="LastName">Smith</Field> 
        <Field name="OriginalHireDt">03211977</Field> 
        <Field name="ParticipationDt">05011977</Field>
        <Field name="Fulltime">False</Field>
    </Group>
</Worker>
<Worker>
    - <Group name="Employee Changes">
        <Field name="LastName">Jones</Field> 
        <Field name="OriginalHireDt">03211977</Field> 
        <Field name="ParticipationDt">05011977</Field>
        <Field name="Fulltime">False</Field>
    </Group>
</Worker>

1 个答案:

答案 0 :(得分:0)

我相信这应该是解决方案,因为你需要每场比赛后休息所以我建议你这样做:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>

<xsl:template match="/">
    <xsl:for-each select="Worker_Sync/Worker/Group[Field[@name='LastName']='Smith' 
        or 
        Field[@name='Fulltime']='True']">            
        <xsl:if test="Field[@name='Fulltime']='True'">
            <xsl:value-of select="Field[@name='LastName']"/>  <xsl:text> - Fulltime</xsl:text>
<xsl:if test="position()!=last()">
            <xsl:text>&#10;</xsl:text>
        </xsl:if>
        </xsl:if>
        <xsl:if test="Field[@name='LastName']='Smith'">
            <xsl:value-of select="upper-case(Field[@name='LastName'])"/>
        </xsl:if>
        <xsl:if test="position()!=last()">
            <xsl:text>&#10;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

如果有效,请告诉我。