从 plm 检索值 xml 使用 xslt 添加它们并在 excel 中显示

retrieving values from plm xml adding them using xslt and displaying in excel

从 teamcenter 结构管理器导出的 plmxml 代码:

    <?xml version="1.0" encoding="utf-8"?>

    <!-- 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>

显示 10 和 15(来自 plm xml)的 xsl 代码,它们显示在相邻的单元格中:

    <?xml version="1.0" encoding="utf-8" ?>

         <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="xml" version="1.0" indent="yes" />
         <xsl:strip-space elements="*"/>


         <xsl:template match="/">
             <xsl:processing-instruction name="mso-application">progid="Excel.Sheet"</xsl:processing-instruction>
            <Workbook 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">

              <Styles>
                <Style ss:ID="s22">
                  <Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
                </Style>
                <Style ss:ID="s23">
                  <Font ss:Color="#FF0000"/>
                </Style>
                <!-- fix me: may be use different color -->
                <Style ss:ID="s24">
                  <Font ss:Color="#FF0000"/>
                </Style>
              </Styles>

              <Worksheet ss:Name="Sheet1">

                <Table>
                  <Column ss:AutoFitWidth="0" ss:Width="150" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="150" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="150" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="100" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="100" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="100" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="100" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="100" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="100" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="100" AutoFitWidth="1"/>
                  <Column ss:AutoFitWidth="0" ss:Width="100" AutoFitWidth="1"/>
                <Row>
                    <Cell ss:StyleID="s22">
                      <Data ss:Type="String">
                        Assembly
                      </Data>
                    </Cell>
                  </Row>

                   <Row>
                   <Cell ss:StyleID="s22">
                      <Data ss:Type="String">

                      </Data>
                    </Cell>
                    <Cell ss:StyleID="s22">
                      <Data ss:Type="String">
                        PartA
                      </Data>
                    </Cell>
                    <Cell ss:StyleID="s22">
                      <Data ss:Type="String">
                       PartB
                      </Data>
                    </Cell>
                     <Cell ss:StyleID="s22">
                      <Data ss:Type="String">
                       PartA+PartB
                      </Data>
                    </Cell>
                  </Row>


                   <xsl:variable name="sum" select="'0'" />
                  <xsl:for-each select="plm:PLMXML/plm:ProductView/plm:Occurrence/plm:UserData/plm:UserValue[@title='bl_quantity']">
                  <xsl:variable name="a" select="@value" />
                  <xsl:variable name="sum" select="$sum + $a" />
                  </xsl:for-each>

                  <Row> 
                        <xsl:for-each select="plm:PLMXML/plm:ProductView/plm:Occurrence/plm:UserData/plm:UserValue[@title='bl_quantity']">
                        <Cell ss:StyleID="s22">
                        <Data ss:Type="String">

                        <xsl:value-of select="@value"/>

                    </Data>
                         </Cell>
                         </xsl:for-each> 
                         <Cell ss:StyleID="s22">
                       <Data ss:Type="String">
                       <xsl:value-of select="$sum"/>
                      </Data>
                    </Cell>  
                  </Row>    

                </Table>
                <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
                  <Selected/>
                  <ProtectObjects>False</ProtectObjects>
                  <ProtectScenarios>False</ProtectScenarios>
                </WorksheetOptions>
              </Worksheet>
            </Workbook>

          </xsl:template>

        </xsl:stylesheet>

我可以在 excel 中显示 10 和 15,但添加部分不起作用。请帮我解决这个问题。

在 XSLT 中,变量是不可变的,不能更改。这意味着您增加 sum 的代码无法按预期工作

<xsl:variable name="sum" select="'0'" />
<xsl:for-each select="plm:PLMXML/plm:ProductView/plm:Occurrence/plm:UserData/plm:UserValue[@title='bl_quantity']">
    <xsl:variable name="a" select="@value" />
    <xsl:variable name="sum" select="$sum + $a" />
</xsl:for-each>

事实上,这甚至可能被认为是 XSLT 1.0 中的一个错误,因为根据 W3C specification "It is an error if a binding established by an xsl:variable or xsl:param element within a template shadows another binding established by an xsl:variable or xsl:param element also within the template". (It is not an error in XSLT 2.0, but the second sum is local to the xsl:for-each only, and different to the first sum. See http://www.w3.org/TR/xslt20/#scope-of-variables)

无论如何,要解决这个问题,您应该使用 sum 函数。而不是做

<xsl:value-of select="$sum"/>

这样做

<xsl:value-of 
     select="sum(plm:PLMXML/plm:ProductView/plm:Occurrence/plm:UserData/
                 plm:UserValue[@title='bl_quantity' and normalize-space(@value)]/@value)"/>

实际上,您可以通过在此处使用变量来简化事情。试试这个 XSLT 片段

<xsl:variable name="bl_quantity" select="plm:PLMXML/plm:ProductView/plm:Occurrence/plm:UserData/plm:UserValue[@title='bl_quantity']" />
<Row> 
    <xsl:for-each select="$bl_quantity">
        <Cell ss:StyleID="s22">
            <Data ss:Type="String">
                <xsl:value-of select="@value"/>
            </Data>
        </Cell>
    </xsl:for-each> 
    <Cell ss:StyleID="s22">
       <Data ss:Type="String">
        <xsl:value-of select="sum($bl_quantity[normalize-space(@value)]/@value)"/>
      </Data>
    </Cell>  
</Row>