从plm xml中检索值,使用xslt添加它们并在excel中显示 [英] retrieving values from plm xml adding them using xslt and displaying in excel

查看:253
本文介绍了从plm xml中检索值,使用xslt添加它们并在excel中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<! - GENERATED BY:PLM XML SDK 7.0.3.285 - >
< PLMXML xmlns =http://www.plmxml.org/Schemas/PLMXMLSchema
schemaVersion =6language =en-usdate =2014-12-30时间=18:11:34author =Teamcenter V10000.1.0.20130604.00 - infodba @ IMC - 1989821519(-1989821519)>
< Header id =id1traverseRootRefs =#id7transferContext =new_transfermode>< / Header>
< RevisionRule id =id2name =最新工作>
<描述&最新工作其他最新任何状态< / Description>
< ApplicationRef version =QEaRaYqhYa1ubAapplication =Teamcenterlabel =QEaRaYqhYa1ubA>< / ApplicationRef>< / RevisionRule>
< ProductView id =id4ruleRefs =#id2rootRefs =id7primaryOccurrenceRef =id7>
< ApplicationRef application =Teamcenterlabel =QvfRqkT9Ya1ubA / QEaRaYqhYa1ubA / AAAAAAAAAAAAAA / BOM>< / ApplicationRef>
< UserData id =id3type =TC Specific Properties>
< UserValue value =imprecisetitle =BOM_precision_type>< / UserValue>< / UserData>
< Occurrence id =id7occurrenceRefs =id11 id15>
< ApplicationRef application =Teamcenterlabel =QvfRqkT9Ya1ubA />< / ApplicationRef>
< UserData id =id6>
< UserValue value =title =bl_quantity>< / UserValue>< / UserData>
< UserData id =id8type =AttributesInContext>
< UserValue value =title =AO_ID>< / UserValue>
< UserValue value =title =SequenceNumber>< / UserValue>
< UserValue value =title =OccurrenceName>< / UserValue>
< UserValue value =title =Quantity>< / UserValue>< / UserData>
<变换id =id5> 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1< /变换>< / Occurrence>
< Occurrence id =id11parentRef =#id7>
< ApplicationRef application =Teamcenterlabel =QvfRqkT9Ya1ubA / Q3YRqkT9Ya1ubA />< / ApplicationRef>
< UserData id =id10>
< UserValue value =10title =bl_quantity>< / UserValue>< / UserData>
< UserData id =id12type =AttributesInContext>
< UserValue value =title =AO_ID>< / UserValue>
< UserValue value =10title =SequenceNumber>< / UserValue>
< UserValue value =title =OccurrenceName>< / UserValue>
< UserValue value =10title =Quantity>< / UserValue>< / UserData>
<变换id =id9> 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1< /变换>< / Occurrence>
< Occurrence id =id15parentRef =#id7>
< ApplicationRef application =Teamcenterlabel =QvfRqkT9Ya1ubA / gTVRqkT9Ya1ubA />< / ApplicationRef>
< UserData id =id14>
< UserValue value =15title =bl_quantity>< / UserValue>< / UserData>
< UserData id =id16type =AttributesInContext>
< UserValue value =title =AO_ID>< / UserValue>
< UserValue value =20title =SequenceNumber>< / UserValue>
< UserValue value =title =OccurrenceName>< / UserValue>
< UserValue value =15title =Quantity>< / UserValue>< / UserData>
<变换id =id13> 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1< /变换>< / Occurrence>< / ProductView>< / PLMXML>

xsl代码显示10& 15(从plm xml),它们显示在相邻单元格中:

 <?xml version =1.0encoding =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 =xmlversion =1.0indent =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 $b $ b xmlns:html =http:// www。。 w3.org/TR/REC-html40\">

<样式>
<样式ss:ID =s22>
<内部ss:Color =#C0C0C0ss:Pattern =Solid/>
< / Style>
<样式ss:ID =s23>
<字体ss:Color =#FF0000/>
< / Style>
<! - 修复我:可能使用不同的颜色 - >
<样式ss:ID =s24>
<字体ss:Color =#FF0000/>
< / Style>
< / Styles>

<工作表ss:Name =Sheet1>

<表>
<列ss:AutoFitWidth =0ss:Width =150AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =150AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =150AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =100AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =100AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =100AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =100AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =100AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =100AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =100AutoFitWidth =1/>
<列ss:AutoFitWidth =0ss:Width =100AutoFitWidth =1/>
< Row>
<单元格ss:StyleID =s22>
<数据ss:Type =String>
汇编
< / Data>
< / Cell>
< / Row>

< Row>
<单元格ss:StyleID =s22>
<数据ss:Type =String>

< / Data>
< / Cell>
<单元格ss:StyleID =s22>
<数据ss:Type =String>
PartA
< / Data>
< / Cell>
<单元格ss:StyleID =s22>
<数据ss:Type =String>
PartB
< / Data>
< / Cell>
<单元格ss:StyleID =s22>
<数据ss:Type =String>
PartA + PartB
< / Data>
< / Cell>
< / Row>


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

< Row>
< xsl:for-each select =plm:PLMXML / plm:ProductView / plm:Occurrence / plm:UserData / plm:UserValue [@ title ='bl_quantity']>
<单元格ss:StyleID =s22>
<数据ss:Type =String>

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

< / Data>
< / Cell>
< / xsl:for-each>
<单元格ss:StyleID =s22>
<数据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>

我可以显示10& 15在excel但添加部分是不工作。请帮助我。

解决方案

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

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

事实上,这甚至可能被认为是XSLT 1.0中的错误,因为根据 W3C规范如果由xsl:变量建立的绑定或模板中的xsl:param元素会影响xsl:variable或xsl:param元素在模板中建立的另一个绑定。 (这不是XSLT 2.0中的错误,但第二个总和仅在 xsl:for-each 的本地,不同于第一个 sum 。请参见 http://www.w3.org/TR/xslt20/#scope-of-variables



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

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

这样做

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

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

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


plmxml code exported from teamcenter structure manager:

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

xsl code to display 10 & 15(from plm xml) and them display in adjacent cell:

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

I am able to display 10 & 15 in excel but adding part is not working. Please help me with this.

解决方案

In XSLT, variables are immutable, and cannot be changed. This means your code to increment sum does not work as you expect

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

In fact, this may even be considered an error in XSLT 1.0, because according to the 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)

Anyway, to solve this, you should be using the sum function. Instead of doing

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

Do this

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

Actually, you could simplify things by making use of a variable here. Try this XSLT snippet

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

这篇关于从plm xml中检索值,使用xslt添加它们并在excel中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆