栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过Java中的XSLT分解XML

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过Java中的XSLT分解XML

给定以下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,您可以控制更多,但还必须重新定义元素…



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507189.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号