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

how to transform plm xml into excel using xslt

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

<?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 sheet 中显示值 10 和 15(bl_quantity)。但是我没有得到任何输出。 value-of select 的语法是否正确?

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

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

最后,您的 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>