给定以下XML:
<?xml version="1.0" encoding="utf-8" ?><Employee name="A Name"> <Address>123 A Street</Address> <Age>28</Age> <EmploymentHistory> <Employment country="US"> <Comment>List of previous jobs in the US</Comment> <Jobs>3</Jobs> <JobDetails> <Job title = "Developer"> <StartDate>01/10/2001</StartDate> <Months>38</Months> </Job> <Job title = "Developer"> <StartDate>01/12/2004</StartDate> <Months>6</Months> </Job> <Job title = "Developer"> <StartDate>01/06/2005</StartDate> <Months>10</Months> </Job> </JobDetails> </Employment> <Employment country="UK"> <Comment>List of previous jobs in the UK</Comment> <Jobs>2</Jobs> <JobDetails> <Job title = "Developer"> <StartDate>01/05/1999</StartDate> <Months>25</Months> </Job> <Job title = "Developer"> <StartDate>01/07/2001</StartDate> <Months>3</Months> </Job> </JobDetails> </Employment> </EmploymentHistory> <Available>true</Available> <Experience unit="years">6</Experience></Employee>
以下XSLT:
<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <Output> <xsl:apply-templates select="//Employee/EmploymentHistory/Employment/JobDetails/Job" /> </Output> </xsl:template> <xsl:template match="//Employee/EmploymentHistory/Employment/JobDetails/Job"> <Employee> <xsl:attribute name="name"> <xsl:value-of select="ancestor::Employee/@name"/> </xsl:attribute> <Address> <xsl:value-of select="ancestor::Employee/Address"/> </Address> <Age> <xsl:value-of select="ancestor::Employee/Age"/> </Age> <EmploymentHistory> <Employment> <xsl:attribute name="country"> <xsl:value-of select="ancestor::Employment/@country"/> </xsl:attribute> <Comment> <xsl:value-of select="ancestor::Employment/Comment"/> </Comment> <Jobs> <xsl:value-of select="ancestor::Employment/Jobs"/> </Jobs> <JobDetails> <xsl:copy-of select="."/> </JobDetails> <Available> <xsl:value-of select="ancestor::Employee/Available"/> </Available> <Experience> <xsl:attribute name="unit"> <xsl:value-of select="ancestor::Employee/Experience/@unit"/> </xsl:attribute> <xsl:value-of select="ancestor::Employee/Experience"/> </Experience> </Employment> </EmploymentHistory> </Employee> </xsl:template></xsl:stylesheet>
提供以下输出:
<?xml version="1.0" encoding="utf-8"?><Output> <Employee name="A Name"> <Address>123 A Street</Address> <Age>28</Age> <EmploymentHistory> <Employment country="US"> <Comment>List of previous jobs in the US</Comment> <Jobs>3</Jobs> <JobDetails> <Job title="Developer"> <StartDate>01/10/2001</StartDate> <Months>38</Months> </Job> </JobDetails> <Available>true</Available> <Experience unit="years">6</Experience> </Employment> </EmploymentHistory> </Employee> <Employee name="A Name"> <Address>123 A Street</Address> <Age>28</Age> <EmploymentHistory> <Employment country="US"> <Comment>List of previous jobs in the US</Comment> <Jobs>3</Jobs> <JobDetails> <Job title="Developer"> <StartDate>01/12/2004</StartDate> <Months>6</Months> </Job> </JobDetails> <Available>true</Available> <Experience unit="years">6</Experience> </Employment> </EmploymentHistory> </Employee> <Employee name="A Name"> <Address>123 A Street</Address> <Age>28</Age> <EmploymentHistory> <Employment country="US"> <Comment>List of previous jobs in the US</Comment> <Jobs>3</Jobs> <JobDetails> <Job title="Developer"> <StartDate>01/06/2005</StartDate> <Months>10</Months> </Job> </JobDetails> <Available>true</Available> <Experience unit="years">6</Experience> </Employment> </EmploymentHistory> </Employee> <Employee name="A Name"> <Address>123 A Street</Address> <Age>28</Age> <EmploymentHistory> <Employment country="UK"> <Comment>List of previous jobs in the UK</Comment> <Jobs>2</Jobs> <JobDetails> <Job title="Developer"> <StartDate>01/05/1999</StartDate> <Months>25</Months> </Job> </JobDetails> <Available>true</Available> <Experience unit="years">6</Experience> </Employment> </EmploymentHistory> </Employee> <Employee name="A Name"> <Address>123 A Street</Address> <Age>28</Age> <EmploymentHistory> <Employment country="UK"> <Comment>List of previous jobs in the UK</Comment> <Jobs>2</Jobs> <JobDetails> <Job title="Developer"> <StartDate>01/07/2001</StartDate> <Months>3</Months> </Job> </JobDetails> <Available>true</Available> <Experience unit="years">6</Experience> </Employment> </EmploymentHistory> </Employee></Output>
请注意,我添加了Output根元素以确保文档格式正确。
这就是你想要的吗?
您也许还可以使用xsl:copy复制更高级别的元素,但是我需要再考虑一点。使用上面的xslt,您可以控制更多,但还必须重新定义元素…



