如何使用xslt将plm xml转换为excel

时间:2015-01-07 12:22:03

标签: xml excel xslt xml-parsing

我从teamcenter结构管理器导出的plm xml代码在这里

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="check_new.xsl"?>
<!-- GENERATED BY: PLM XML SDK 7.0.3.285 -->
<PLMXML xmlns="http://www.plmxml.org/Schemas/PLMXMLSchema"
 schemaVersion="6" language="en-us" date="2014-12-30" time="18:11:34" author="Teamcenter V10000.1.0.20130604.00 - infodba@IMC--1989821519(-1989821519)">
<Header id="id1" traverseRootRefs="#id7" transferContext="new_transfermode"></Header>
<RevisionRule id="id2" name="Latest Working">
<Description>Latest Working else Latest Any Status</Description>
<ApplicationRef version="QEaRaYqhYa1ubA" application="Teamcenter" label="QEaRaYqhYa1ubA"></ApplicationRef></RevisionRule>
<ProductView id="id4" ruleRefs="#id2" rootRefs="id7" primaryOccurrenceRef="id7">
<ApplicationRef application="Teamcenter" label="QvfRqkT9Ya1ubA/QEaRaYqhYa1ubA/AAAAAAAAAAAAAA/BOM"></ApplicationRef>
<UserData id="id3" type="TC Specific Properties">
<UserValue value="imprecise" title="BOM_precision_type"></UserValue></UserData>
<Occurrence id="id7" occurrenceRefs="id11 id15">
<ApplicationRef application="Teamcenter" label="QvfRqkT9Ya1ubA/"></ApplicationRef>
<UserData id="id6">
<UserValue value="" title="bl_quantity"></UserValue></UserData>
<UserData id="id8" type="AttributesInContext">
<UserValue value="" title="AO_ID"></UserValue>
<UserValue value="" title="SequenceNumber"></UserValue>
<UserValue value="" title="OccurrenceName"></UserValue>
<UserValue value="" title="Quantity"></UserValue></UserData>
<Transform id="id5">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</Transform></Occurrence>
<Occurrence id="id11" parentRef="#id7">
<ApplicationRef application="Teamcenter" label="QvfRqkT9Ya1ubA/Q3YRqkT9Ya1ubA/"></ApplicationRef>
<UserData id="id10">
<UserValue **value="10"** title="bl_quantity"></UserValue></UserData>
<UserData id="id12" type="AttributesInContext">
<UserValue value="" title="AO_ID"></UserValue>
<UserValue value="10" title="SequenceNumber"></UserValue>
<UserValue value="" title="OccurrenceName"></UserValue>
<UserValue value="10" title="Quantity"></UserValue></UserData>
<Transform id="id9">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</Transform></Occurrence>
<Occurrence id="id15" parentRef="#id7">
<ApplicationRef application="Teamcenter" label="QvfRqkT9Ya1ubA/gTVRqkT9Ya1ubA/"></ApplicationRef>
<UserData id="id14">
<UserValue **value="15"** title="bl_quantity"></UserValue></UserData>
<UserData id="id16" type="AttributesInContext">
<UserValue value="" title="AO_ID"></UserValue>
<UserValue value="20" title="SequenceNumber"></UserValue>
<UserValue value="" title="OccurrenceName"></UserValue>
<UserValue value="15" title="Quantity"></UserValue></UserData>
<Transform id="id13">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</Transform></Occurrence></ProductView></PLMXML>

xsl代码:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
           xmlns:plm="http://www.plmxml.org/Schemas/PLMXMLSchema"
           xmlns="urn:schemas-microsoft-com:office:spreadsheet"
           xmlns:o="urn:schemas-microsoft-com:office:office"
           xmlns:x="urn:schemas-microsoft-com:office:excel"
           xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
           xmlns:html="http://www.w3.org/TR/REC-html40" >
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Admin - Item Ownership</title>
      </head>
      <body BGCOLOR="#FFFFFF" link="#0000FF" vlink="#660066">
        <table>
          <tr align="center" bgcolor="#B8CFEP">
            <!-- 1st row -->
            <th>part a</th>         
          </tr>
       <tr>
      <td>
        <xsl:value-of select="/plm:PLMXML/plm:Header/plm:ProductView/plm:UserData/plm:UserValue/title=bl_quantity"/>
      </td> </tr>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

我需要在Excel工作表中显示值10和15(bl_quantity)。但我没有得到任何输出。 select-value的语法是对的吗?

1 个答案:

答案 0 :(得分:0)

首先,您不是生成Excel文档,而是生成HTML表格。我想Excel可以打开它,但需要说明一下。我相信它也使你的几乎所有命名空间声明都变得多余。

第二件事是你应该为每个匹配的值都有一个表行。你的样式表只会得到第一个。

最后,您的XPath选择已经过时了。试试这种方式:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:plm="http://www.plmxml.org/Schemas/PLMXMLSchema"
exclude-result-prefixes="plm">

<xsl:template match="/">
    <html>
        <head>
            <title>Admin - Item Ownership</title>
        </head>
        <body BGCOLOR="#FFFFFF" link="#0000FF" vlink="#660066">
            <table>
                <tr align="center" bgcolor="#B8CFEP">
                    <th>part a</th>         
                </tr>
                <xsl:for-each select="plm:PLMXML/plm:ProductView/plm:Occurrence/plm:UserData/plm:UserValue[@title='bl_quantity']">
                    <tr>
                        <td>
                            <xsl:value-of select="@value"/>
                        </td>
                    </tr>
                </xsl:for-each> 
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>
相关问题