I am trying to export a spreadsheet to a specific xml layout to be imported into a pdf form. I have gotten pretty far on my own but am stuck at what seems to be near the end. (Mind you, I am not very proficient with programing stuff, and my experience with XML/XSLT is limited to this project)
First, I exported an example set of data from the pdf, which gave me an idea of what I am looking for. I then imported this into Excel and got a spreadsheet from the data. This let me edit the data easily, then theoretically export it back, but...Excel wouldn't export the XML map that it created on import due to a "lists of lists" problem. This led me to putting the speadsheet into OpenOffice (technically LibreOffice) and trying an开发者_StackOverflow社区 XML export filter using XSLT.
My data in the spreadsheet looks like...
Type Name Compound Weight Material Weight
AAA BBB X 5 s 2
AAA BBB X 5 t 3
AAA BBB Y 4 r 4
I need it to be exported as like...
<?xml version="1.0" encoding="UTF-8" ?>
- <MCD Type="AAA" Name="BBB">
- <Product Compound="X">
<Amount weight="5"/>
- <HM Material="s">
<Amount weight="2" />
</HM>
- <HM Material="t">
<Amount weight="3" />
</HM>
</Product>
- <Product Compound="Y">
<Amount weight="4"/>
- <HM Material="r">
<Amount weight="4" />
</HM>
</Product>
</MCD>
But with my current XSL, I get something more like...
<?xml version="1.0" encoding="UTF-8" ?>
- <MCD Type="AAA" Name="BBB">
- <Product Compound="X">
<Amount weight="5"/>
- <HM Material="s">
<Amount weight="2" />
</HM>
</Product>
</MCD>
- <MCD Type="AAA" Name="BBB">
- <Product Compound="X">
- <HM Material="t">
<Amount weight="3" />
</HM>
</Product>
</MCD>
- <MCD Type="AAA" Name="BBB">
- <Product Compound="Y">
<Amount weight="4"/>
- <HM Material="r">
<Amount weight="4" />
</HM>
</Product>
</MCD>
My problem lies with each cell of the spreadsheet being in the output. I want to have the parent cell only be deiplayed when it changes, like in a folder tree. I hope this makes sense and someone can help me (either making it work in excel or openoffice, though from my search it seems the OpenOffice route is more likely). I have scoured for answers but nothing has quite answered my needs. Thank you in advance.
if you're using XSLT 2, starting from your last XML and using the following stylesheet:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each-group select="//MCD" group-by="concat(@Type,'-',@Name)">
<MCD Type="{@Type}" Name="{@Name}">
<xsl:for-each select="//Product[(parent::MCD/@Type = substring-before(current-grouping-key(),'-')) and (parent::MCD/@Name = substring-after(current-grouping-key(),'-'))]">
<xsl:copy-of select="." />
</xsl:for-each>
</MCD>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
you ll get:
<?xml version="1.0" encoding="UTF-8"?><MCD Type="AAA" Name="BBB"><Product Compound="X">
<Amount weight="5"/>
<HM Material="s">
<Amount weight="2"/>
</HM>
</Product><Product Compound="X">
<HM Material="t">
<Amount weight="3"/>
</HM>
</Product><Product Compound="Y">
<Amount weight="4"/>
<HM Material="r">
<Amount weight="4"/>
</HM>
</Product></MCD>
精彩评论