XSLT2.0-如何在输出文件中默认包含属性?

XSLT2.0-如何在输出文件中默认包含属性?,xslt,xslt-2.0,Xslt,Xslt 2.0,我希望在输入文件中存在的输出文件中包含属性。期望值来自以下输入文件,转换后的输出文件中包含属性ws:PriorValue 输入文件: <?xml version="1.0" encoding="UTF-8"?> <ws:Worker_Sync xmlns:ws="urn:com.workday/workersync" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ws:Worker>

我希望在输入文件中存在的输出文件中包含属性。期望值来自以下输入文件,转换后的输出文件中包含属性ws:PriorValue

输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<ws:Worker_Sync xmlns:ws="urn:com.workday/workersync" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ws:Worker>
        <ws:Summary>
            <ws:Employee_ID>00013582</ws:Employee_ID>
            <ws:Name>Test Employee</ws:Name>
        </ws:Summary>       
        <ws:Personal>
            <ws:Name_Data>
                <ws:Name_Type>Legal</ws:Name_Type>
                <ws:First_Name>Test</ws:First_Name>
                <ws:Last_Name ws:PriorValue="Worker">Employee</ws:Last_Name>                
            </ws:Name_Data>         
            <ws:Gender ws:PriorValue="">Male</ws:Gender>
            <ws:Birth_Date>1985-12-21</ws:Birth_Date>
            <ws:Address_Data>
                <ws:Address_Type>HOME</ws:Address_Type>
                <ws:Address_Is_Public>false</ws:Address_Is_Public>
                <ws:Is_Primary>true</ws:Is_Primary>
                <ws:Address_Line_Data ws:PriorValue="Address test">Address Sample</ws:Address_Line_Data>
                <ws:Submunicipality ws:PriorValue="Helsinki">Tampere</ws:Submunicipality>
                <ws:Postal_Code ws:PriorValue="00010">01350</ws:Postal_Code>
                <ws:Country>FI</ws:Country>
            </ws:Address_Data>          
        </ws:Personal>
        <ws:Contract>
            <ws:Operation>ADD</ws:Operation>
            <ws:Position_ID ws:PriorValue="P5002970612">P5002970643</ws:Position_ID>
            <ws:Contract_Type ws:PriorValue="Temporary">Permanent</ws:Contract_Type>
            <ws:Start_Date ws:PriorValue="2017-01-01">2017-08-31</ws:Start_Date>            
        </ws:Contract>
    </ws:Worker>
</ws:Worker_Sync>

00013582
测试员工
合法的
测验
受雇者
男性的
1985-12-21
家
错误的
符合事实的
地址样本
坦佩雷
01350
FI
添加
P5002970643
永久的
2017-08-31            
到目前为止我创建的XSLT:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ws="urn:com.workday/workersync" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
    <!--xsl:output encoding="utf-16"/-->
    <xsl:template match="ws:Worker_Sync">
        <Root>
            <xsl:apply-templates select="ws:Worker">
            </xsl:apply-templates>
        </Root>
    </xsl:template>
    <xsl:template match="ws:Worker">
        <Worker>
            <Personal>
                <BirthDate>
                    <xsl:value-of select=" format-date(xs:date(ws:Personal/ws:Birth_Date), '[Y0001][M01][D01]') "/>
                </BirthDate>
                <FirstName>
                    <xsl:value-of select="concat(ws:Personal/ws:Name_Data[ws:Name_Type='Legal']/ws:First_Name, ws:Personal/ws:Name_Data[ws:Name_Type='Legal']/ws:Middle_Name)"/>
                </FirstName>
                <Gender>
                    <xsl:value-of select="if (ws:Personal/ws:Gender='Male') then '1' else '2'"/>
                </Gender>
                <GenderText>
                    <xsl:value-of select="ws:Personal/ws:Gender"/>
                </GenderText>
                <Initials>
                    <xsl:value-of select="concat(ws:Personal/ws:Name_Data[ws:Name_Type='Preferred']/ws:First_Name, ' ', ws:Personal/ws:Name_Data[ws:Name_Type='Preferred']/ws:Last_Name)"/>
                </Initials>
                <Language>
                    <xsl:value-of select="ws:Personal/ws:Birth_Date"/>
                </Language>
                <LastName>
                    <xsl:value-of select="ws:Personal/ws:Name_Data[ws:Name_Type='Legal']/ws:Last_Name"/>
                </LastName>
                <Ssn>
                    <xsl:value-of select="ws:Identification_Data[ws:Identification='National']/ws:ID"/>
                </Ssn>
            </Personal>
            <Contact_Details>
                <CityHome>
                    <xsl:value-of select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Submunicipality"/>
                </CityHome>
                <CountryHome>
                    <xsl:value-of select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Country"/>
                </CountryHome>              
                <StreetAddressHome>
                    <xsl:value-of select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Address_Line_Data"/>
                </StreetAddressHome>
                <ZipCodeHome>
                    <xsl:value-of select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Postal_Code"/>
                </ZipCodeHome>                              
            </Contact_Details>
            <Contract>
                <ContractType>
                    <xsl:value-of select="if (ws:Position/ws:Worker_Type = 'Permanent') then '01' else '02'"/>      
                </ContractType>
                <ContractTypeText>
                    <xsl:value-of select="if (ws:Position/ws:Worker_Type = 'Permanent') then 'Vakituinen' else 'Määräaik. sopimus'"/>               
                </ContractTypeText>
                <ProbationEndDate>
                    <xsl:value-of select="format-date(xs:date(ws:Status/ws:Probation_End_Date), '[Y0001][M01][D01]')"/>         
                </ProbationEndDate>                 
            </Contract>
        </Worker>
    </xsl:template>
</xsl:stylesheet>



输出:


<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:ws="urn:com.workday/workersync" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Worker>
        <Personal>
            <BirthDate>19851221</BirthDate>
            <FirstName>Test</FirstName>
            <Gender>1</Gender>
            <GenderText>Male</GenderText>
            <Initials> </Initials>
            <Language>1985-12-21</Language>
            <LastName>Employee</LastName>
            <Ssn/>
        </Personal>
        <Contact_Details>
            <CityHome>Tampere</CityHome>
            <CountryHome>FI</CountryHome>
            <StreetAddressHome>Address Sample</StreetAddressHome>
            <ZipCodeHome>01350</ZipCodeHome>
        </Contact_Details>
        <Contract>
            <ContractType>02</ContractType>
            <ContractTypeText>Määräaik. sopimus</ContractTypeText>
            <ProbationEndDate/>
        </Contract>
    </Worker>
</Root>

19851221
测验
1.
男性的
1985-12-21
受雇者
坦佩雷
FI
地址样本
01350
02
Märäaik。索皮莫斯
但我希望得到如下输出文件:

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:ws="urn:com.workday/workersync" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Worker>
        <Personal>
            <BirthDate>19851221</BirthDate>
            <FirstName>Test</FirstName>
            <Gender>1</Gender>
            <GenderText PriorValue="">Male</GenderText>
            <Initials> </Initials>
            <Language>1985-12-21</Language>
            <LastName PriorValue="Worker">Employee</LastName>
            <Ssn/>
        </Personal>
        <Contact_Details>
            <CityHome PriorValue="Helsinki">Tampere</CityHome>
            <CountryHome>FI</CountryHome>
            <StreetAddressHome PriorValue="Address test">Address Sample</StreetAddressHome>
            <ZipCodeHome PriorValue="00010">01350</ZipCodeHome>
        </Contact_Details>
        <Contract>
            <ContractType>02</ContractType>
            <ContractTypeText>Määräaik. sopimus</ContractTypeText>
            <ProbationEndDate/>
        </Contract>
    </Worker>
</Root>


19851221
测验
1.
男性的
1985-12-21
受雇者
坦佩雷
FI
地址样本
01350
02
Märäaik。索皮莫斯

这可能吗?我不确定是否有办法达到这个结果。谢谢您的帮助。

您可以在这里使用
xsl:attribute
元素

<ZipCodeHome>
    <xsl:attribute name="PriorValue" select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Postal_Code/@ws:PriorValue" />
    <xsl:value-of select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Postal_Code"/>
</ZipCodeHome>
然后你会写这个

<ZipCodeHome>
    <xsl:apply-templates select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Postal_Code/@*" />
    <xsl:value-of select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Postal_Code"/>
</ZipCodeHome>

但是,在这两种情况下,XSLT都有很多与XPath表达式的重复,所以考虑使用模板匹配来缩短事情…

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ws="urn:com.workday/workersync" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
    <xsl:output encoding="utf-16" method="xml" indent="yes" />

    <xsl:template match="ws:Worker_Sync">
        <Root>
            <xsl:apply-templates select="ws:Worker" />
        </Root>
    </xsl:template>

    <xsl:template match="ws:Worker">
        <Worker>
            <xsl:apply-templates select="ws:Personal" />
            <xsl:apply-templates select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']" />
            <Contract>
                <ContractType>
                    <xsl:value-of select="if (ws:Position/ws:Worker_Type = 'Permanent') then '01' else '02'"/>      
                </ContractType>
                <ContractTypeText>
                    <xsl:value-of select="if (ws:Position/ws:Worker_Type = 'Permanent') then 'Vakituinen' else 'Määräaik. sopimus'"/>               
                </ContractTypeText>
                <ProbationEndDate>
                    <xsl:value-of select="format-date(xs:date(ws:Status/ws:Probation_End_Date), '[Y0001][M01][D01]')"/>         
                </ProbationEndDate>                 
            </Contract>
        </Worker>
    </xsl:template>

    <xsl:template match="ws:Personal">
        <Personal>
            <BirthDate>
                <xsl:value-of select="format-date(xs:date(ws:Birth_Date), '[Y0001][M01][D01]') "/>
            </BirthDate>
            <FirstName>
                <xsl:value-of select="concat(ws:Name_Data[ws:Name_Type='Legal']/ws:First_Name, ws:Name_Data[ws:Name_Type='Legal']/ws:Middle_Name)"/>
            </FirstName>
            <Gender>
                <xsl:value-of select="if (ws:Gender='Male') then '1' else '2'"/>
            </Gender>
            <GenderText>
                <xsl:apply-templates select="ws:Gender"/>
            </GenderText>
            <Initials>
                <xsl:value-of select="concat(ws:Name_Data[ws:Name_Type='Preferred']/ws:First_Name, ' ', ws:Name_Data[ws:Name_Type='Preferred']/ws:Last_Name)"/>
            </Initials>
            <Language>
                <xsl:apply-templates select="ws:Birth_Date"/>
            </Language>
            <LastName>
                <xsl:apply-templates select="ws:Name_Data[ws:Name_Type='Legal']/ws:Last_Name"/>
            </LastName>
            <Ssn>
                <xsl:apply-templates select="ws:Identification_Data[ws:Identification='National']/ws:ID"/>
            </Ssn>
        </Personal>        
    </xsl:template>


    <xsl:template match="ws:Address_Data">
        <Contact_Details>
            <CityHome>
                <xsl:apply-templates select="ws:Submunicipality" />
            </CityHome>
            <CountryHome>
                <xsl:apply-templates select="ws:Country"/>
            </CountryHome>              
            <StreetAddressHome>
                <xsl:apply-templates select="ws:Address_Line_Data"/>
            </StreetAddressHome>
            <ZipCodeHome>
                <xsl:apply-templates select="ws:Postal_Code"/>
            </ZipCodeHome>                              
        </Contact_Details>        
    </xsl:template>

    <xsl:template match="*">
        <xsl:apply-templates select="@*" />
        <xsl:value-of select="."/>
    </xsl:template>

    <xsl:template match="@*">
      <xsl:attribute name="{local-name()}" select="." />
    </xsl:template>
</xsl:stylesheet>


谢谢你,它起了很大的作用。我希望能提高一点。所以只有PriorValue属性,没有其他属性。我试图转换,只要输入文件中有PriorValue属性,那么输出文件在转换的节点/字段中就必须更改为true。通过更改attribute name=“Changed”为我提供更改的属性,但如何将其默认为值“true”。使用select='true'没有帮助。请你帮忙
如果您想要一个名为“Changed”且值为“true”的属性,请执行
(注意在选择值中使用单撇号)。
<ZipCodeHome>
    <xsl:apply-templates select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Postal_Code/@*" />
    <xsl:value-of select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']/ws:Postal_Code"/>
</ZipCodeHome>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ws="urn:com.workday/workersync" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
    <xsl:output encoding="utf-16" method="xml" indent="yes" />

    <xsl:template match="ws:Worker_Sync">
        <Root>
            <xsl:apply-templates select="ws:Worker" />
        </Root>
    </xsl:template>

    <xsl:template match="ws:Worker">
        <Worker>
            <xsl:apply-templates select="ws:Personal" />
            <xsl:apply-templates select="ws:Personal/ws:Address_Data[ws:Address_Type='HOME']" />
            <Contract>
                <ContractType>
                    <xsl:value-of select="if (ws:Position/ws:Worker_Type = 'Permanent') then '01' else '02'"/>      
                </ContractType>
                <ContractTypeText>
                    <xsl:value-of select="if (ws:Position/ws:Worker_Type = 'Permanent') then 'Vakituinen' else 'Määräaik. sopimus'"/>               
                </ContractTypeText>
                <ProbationEndDate>
                    <xsl:value-of select="format-date(xs:date(ws:Status/ws:Probation_End_Date), '[Y0001][M01][D01]')"/>         
                </ProbationEndDate>                 
            </Contract>
        </Worker>
    </xsl:template>

    <xsl:template match="ws:Personal">
        <Personal>
            <BirthDate>
                <xsl:value-of select="format-date(xs:date(ws:Birth_Date), '[Y0001][M01][D01]') "/>
            </BirthDate>
            <FirstName>
                <xsl:value-of select="concat(ws:Name_Data[ws:Name_Type='Legal']/ws:First_Name, ws:Name_Data[ws:Name_Type='Legal']/ws:Middle_Name)"/>
            </FirstName>
            <Gender>
                <xsl:value-of select="if (ws:Gender='Male') then '1' else '2'"/>
            </Gender>
            <GenderText>
                <xsl:apply-templates select="ws:Gender"/>
            </GenderText>
            <Initials>
                <xsl:value-of select="concat(ws:Name_Data[ws:Name_Type='Preferred']/ws:First_Name, ' ', ws:Name_Data[ws:Name_Type='Preferred']/ws:Last_Name)"/>
            </Initials>
            <Language>
                <xsl:apply-templates select="ws:Birth_Date"/>
            </Language>
            <LastName>
                <xsl:apply-templates select="ws:Name_Data[ws:Name_Type='Legal']/ws:Last_Name"/>
            </LastName>
            <Ssn>
                <xsl:apply-templates select="ws:Identification_Data[ws:Identification='National']/ws:ID"/>
            </Ssn>
        </Personal>        
    </xsl:template>


    <xsl:template match="ws:Address_Data">
        <Contact_Details>
            <CityHome>
                <xsl:apply-templates select="ws:Submunicipality" />
            </CityHome>
            <CountryHome>
                <xsl:apply-templates select="ws:Country"/>
            </CountryHome>              
            <StreetAddressHome>
                <xsl:apply-templates select="ws:Address_Line_Data"/>
            </StreetAddressHome>
            <ZipCodeHome>
                <xsl:apply-templates select="ws:Postal_Code"/>
            </ZipCodeHome>                              
        </Contact_Details>        
    </xsl:template>

    <xsl:template match="*">
        <xsl:apply-templates select="@*" />
        <xsl:value-of select="."/>
    </xsl:template>

    <xsl:template match="@*">
      <xsl:attribute name="{local-name()}" select="." />
    </xsl:template>
</xsl:stylesheet>