2017-06-13 66 views
0

今天我有一个很基本的问题。为什么所有元素的属性不一致?XSLT应用模板不在同一行上

我期望的输出是 “姓氏”, “将First_Name”, “出生日期”(如果Bene_DOB不存在,那么使用DOB_DEP)
像这样:
“吉布森”, “梅尔”,“1965-01- 01"
“诺里斯”, “查克”

但我得到:

“诺里斯”, “吉布森”, “查”, “梅尔”,

1965年1月1日 12345-01

1965年1月1日
12345-01

我有这样的XML

<?xml version='1.0' encoding='UTF-8'?> 
<wd:Report_Data xmlns:wd="urn:com.workday.report/BCBSLA_CR_OFAC_BENE"> 
<wd:Report_Entry> 
<wd:BENE_ALL> 
    <wd:Last_Name>Norris</wd:Last_Name> 
    <wd:First_Name>Chuck</wd:First_Name> 
    <wd:REF_ID>12345-01</wd:REF_ID> 
</wd:BENE_ALL>  
    <wd:Last_Name>Gibson</wd:Last_Name> 
    <wd:First_Name>Mel</wd:First_Name> 
    <wd:REF_ID>12345-02</wd:REF_ID> 
</wd:BENE_ALL> 
<wd:BENE_PEOPLE> 
    <wd:BENE_DOB>1965-01-02</wd:BENE_DOB> 
    <wd:Ben_Ref_ID>12345-01</wd:Ben_Ref_ID> 
</wd:BENE_PEOPLE> 
<wd:BENE_PEOPLE> 
    <wd:BENE_DOB>1955-01-10</wd:BENE_DOB> 
    <wd:Ben_Ref_ID>12345-02</wd:Ben_Ref_ID> 
</wd:BENE_PEOPLE> 
<wd:DEP> 
    <wd:DOB_Dep>1965-01-01</wd:DOB_Dep> 
    <wd:Dep_Ref_ID>12345-01</wd:Dep_Ref_ID> 
</wd:DEP> 
</wd:Report_Entry> 

这里是我的XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
       exclude-result-prefixes="xsl" 
       xmlns:wd="urn:com.workday.report/BCBSLA_CR_OFAC_BENE" 
       version="2.0"> 


    <xsl:output method="text"/> 


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

    <xsl:template match="wd:BENE_ALL"> 
    <xsl:apply-templates select="wd:EMPLID" mode="csv"/> 
    <xsl:apply-templates select="wd:Last_Name" mode="csv"/> 
    <xsl:apply-templates select="wd:First_Name" mode="csv"/> 
    </xsl:template> 


    <xsl:template match="wd:BENE_People"> 
    <xsl:apply-templates select="wd:DOB_Bene" mode="csv" /> 
    </xsl:template> 


    <!--<xsl:choose> 
     <xsl:when test= 
    </xsl:choose>--> 


    <xsl:template match="*" mode="csv"> 
    <xsl:value-of select="concat('&quot;', ., '&quot;,')" /> 
    </xsl:template> 

    <xsl:template match="*" mode="csv-nl"> 
    <xsl:value-of select="concat('&quot;', ., '&quot;&#xA;')" /> 
    </xsl:template> 

</xsl:stylesheet> 

谢谢你的帮助!

+0

你能张贴的例子有多个'wd:BENE_DOB'和'wd:DOB_Dep'? –

回答

1

如果我理解正确的XML的结构,你需要创建一个新的数据行的每个组的元素开始Last_Name。在XSLT 2.0,这可以通过这样做来完成:

<xsl:template match="/Report_Data"> 
    <xsl:for-each-group select="Report_Entry/BENE_ALL/*" group-starting-with="Last_Name"> 
     <xsl:value-of select="concat('&quot;', ., '&quot;,')" /> 
     <xsl:value-of select="concat('&quot;', current-group()[self::First_Name], '&quot;&#10;')" /> 
    </xsl:for-each-group> 
</xsl:template> 

返回:

"Norris","Chuck" 
"Gibson","Mel" 

要添加Bene_DOB值,你可以使用一个key这样的:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xpath-default-namespace="urn:com.workday.report/BCBSLA_CR_OFAC_BENE"> 
<xsl:output method="text" encoding="UTF-8"/> 

<xsl:key name="person-dob" match="BENE_DOB" use="following-sibling::Ben_Ref_ID[1]" /> 

<xsl:template match="/Report_Data"> 
    <xsl:for-each-group select="Report_Entry/BENE_ALL/*" group-starting-with="Last_Name"> 
     <xsl:value-of select="concat('&quot;', ., '&quot;,')" /> 
     <xsl:value-of select="concat('&quot;', current-group()[self::First_Name], '&quot;,')" /> 
     <xsl:value-of select="concat('&quot;', key('person-dob', current-group()[self::REF_ID]), '&quot;&#10;')" /> 
    </xsl:for-each-group> 
</xsl:template> 

</xsl:stylesheet> 

演示:http://xsltransform.net/naZXpX2

这是假设该键中列出了对BENE_DOBBen_Ref_ID


如果你愿意,你可以通过定义功能减少代码的重复:

<xsl:function name="my:quote"> 
    <xsl:param name="text"/> 
    <xsl:sequence select="concat('&quot;', $text, '&quot;')" /> 
</xsl:function> 

然后:

<xsl:template match="/Report_Data"> 
    <xsl:for-each-group select="Report_Entry/BENE_ALL/*" group-starting-with="Last_Name"> 
     <xsl:value-of select="my:quote(.), my:quote(current-group()[self::First_Name]), my:quote(key('person-dob', current-group()[self::REF_ID]))" separator=","/> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:for-each-group> 
</xsl:template> 

演示:http://xsltransform.net/naZXpX2/1

+0

谢谢迈克尔 - 我用价值的改写它,这解决了我的没有出现在一行中的项目的问题。谢谢!虽然我有XSLT 1.0,但关键并不适合我。 – BWatkins

+0

@BWatkins您原来的样式表被标记为'版本=“2。0“,所以我认为这就是你的处理器所支持的。无论如何,这个关键字在XSLT 1.0中都能正常工作,但是'xsl:for-each-group'不能。从你更新的XML判断,你不需要它。 –