XSLT嵌套到Flat结构

时间:2013-07-15 09:35:27

标签: xslt

我是XSLT的新手,我正在努力从嵌套结构转换为平面元素序列。 我的输入如下:

<SyncBillOfMaterialsListEBM>
<EBMHeader/>
<DataArea>
  <Sync/>
  <SyncBillOfMaterialsList/>
  <SyncBillOfMaterialsList>
        <Identification>
            <ID>B31819K</ID>
            <ContextID schemeID="AddedBOMs"/>                         
        </Identification>
        <BillOfMaterialsComponentItem/>
 </SyncBillOfMaterialsList>

 <SyncBillOfMaterialsList>
    <Identification/>
    <BillOfMaterialsComponentItem>
        <Identification>
             <BusinessComponentID schemeID="B31819K"/>
             <ID>B31618R</ID>
             <ContextID schemeID="AddedBOMComponents"/>             
        </Identification>
        <Quantity>5</Quantity>
        <FromUnitNumber>500</FromUnitNumber>
        <TypeCode>IT1</TypeCode>      
        <BillOfMaterialsSubstituteComponentItem/>      
    </BillOfMaterialsComponentItem>   
</SyncBillOfMaterialsList>

<SyncBillOfMaterialsList>
    <Identification/>
    <BillOfMaterialsComponentItem>
        <Identification>
             <BusinessComponentID schemeID="B31819K"/>
             <ID>B31619S</ID>
             <ContextID schemeID="AddedBOMComponents"/>     
        </Identification>
       <Quantity>2</Quantity>
       <FromUnitNumber>600</FromUnitNumber>     
       <TypeCode>IT1</TypeCode>      
    </BillOfMaterialsComponentItem>   
</SyncBillOfMaterialsList>

<SyncBillOfMaterialsList>
    <Identification/>   
    <BillOfMaterialsComponentItem>
        <Identification/>
            <BillOfMaterialsSubstituteComponentItem>
                 <Identification>
                    <BusinessComponentID schemeID="B31819K"/>
                    <ID>B31618R</ID>
                    <ContextID schemeID="AddedSubstitutes"/>                        
                 </Identification>
                <Quantity>5</Quantity>
                <ItemReference>
                    <ItemIdentification>
                       <ID>B31619S</ID>               
                    </ItemIdentification>
                    <SpecificationGroup/>
                </ItemReference>
                <ChangeTypeCode>IT1</ChangeTypeCode>
            </BillOfMaterialsSubstituteComponentItem>      
    </BillOfMaterialsComponentItem>   
</SyncBillOfMaterialsList>

<SyncBillOfMaterialsList>
   <Identification/>       
   <BillOfMaterialsComponentItem>
      <Identification/>       
      <BillOfMaterialsSubstituteComponentItem>
         <Identification>
            <BusinessComponentID schemeID="B31819K"/>
            <ID>B31619S</ID>
            <ContextID schemeID="AddedSubstitutes"/>                
         </Identification>
         <Quantity>2</Quantity>
         <ItemReference>
            <ItemIdentification>
               <ID>B31820L</ID>
                            </ItemIdentification>
         </ItemReference>
         <ChangeTypeCode>IT1</ChangeTypeCode>         
      </BillOfMaterialsSubstituteComponentItem>      
    </BillOfMaterialsComponentItem> 
</SyncBillOfMaterialsList>

输出应该是一个元素列表,如下所示

        <InputParameters>         
        <REV_ITEM>                    
                <REVISED_ITEM_NAME>B31819K</REVISED_ITEM_NAME>
        </REV_ITEML>
        <REV_COMP>      
                <COMPONENT_ITEM_NAME>B31618R</COMPONENT_ITEM_NAME>                
        </REV_COMP>
        <SUB_COMP_TBL_ITEM>                   
                <REVISED_ITEM_NAME>B31819K</REVISED_ITEM_NAME>                    
                <COMPONENT_ITEM_NAME>B31618R</COMPONENT_ITEM_NAME>                   
                <SUBSTITUTE_COMPONENT_NAME>B31619S</SUBSTITUTE_COMPONENT_NAME>
                <SUBSTITUTE_ITEM_QUANTITY>5</SUBSTITUTE_ITEM_QUANTITY>
        </SUB_COMP_TBL_ITEM>
    </InputParameters>

    <InputParameters>         
        <REV_ITEM>                    
                <REVISED_ITEM_NAME>B31819K</REVISED_ITEM_NAME>
        </REV_ITEML>
        <REV_COMP>      
                <COMPONENT_ITEM_NAME>B31618S</COMPONENT_ITEM_NAME>                
        </REV_COMP>
        <SUB_COMP_TBL_ITEM>                   
                <REVISED_ITEM_NAME>B31819K</REVISED_ITEM_NAME>                    
                <COMPONENT_ITEM_NAME>B31618S</COMPONENT_ITEM_NAME>                   
                <SUBSTITUTE_COMPONENT_NAME>B31619L</SUBSTITUTE_COMPONENT_NAME>
                <SUBSTITUTE_ITEM_QUANTITY>2</SUBSTITUTE_ITEM_QUANTITY>
        </SUB_COMP_TBL_ITEM>
    </InputParameters>

我的XSLT根本不工作,因为每次只在一个元素中迭代,我找不到迭代所有元素并在同一结构中获得正确值的方法。

 <xsl:template match="/">
 <xsl:for-each  select="/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList">
 <xsl:choose>
    <xsl:when test="/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/BusinessComponentID/@schemeID != '' AND /SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/ID!='' AND /SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/BillOfMaterialsSubstituteComponentItem/ItemReference/ItemIdentification/ID!='' AND /SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/Identification/ID=/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/BusinessComponentID@schemeID AND /SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/BusinessComponentID@schemeID=/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/BillOfMaterialsSubstituteComponentItem/ItemReference/ItemIdentification/ID >
    <REV_ITEM>                    
                <REVISED_ITEM_NAME><xsl:value-of select="SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/Identification/ID"/></REVISED_ITEM_NAME>
    </REV_ITEML>
    <REV_COMP>      
                <COMPONENT_ITEM_NAME><xsl:value-of select="/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/ID" /></COMPONENT_ITEM_NAME>                
    </REV_COMP>
    <SUB_COMP_TBL_ITEM>                   
                <REVISED_ITEM_NAME><xsl:value-of select="SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/Identification/ID"/></REVISED_ITEM_NAME>                    
                <COMPONENT_ITEM_NAME><xsl:value-of select="/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/ComponentItemProcessingInstruction/Identification/ID" /></COMPONENT_ITEM_NAME>                   
                <SUBSTITUTE_COMPONENT_NAME><xsl:value-of select="/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/BillOfMaterialsSubstituteComponentItem/ItemReference/ItemIdentification/ID"/></SUBSTITUTE_COMPONENT_NAME>
                <SUBSTITUTE_ITEM_QUANTITY><xsl:value-of select="/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/BillOfMaterialsSubstituteComponentItem/Quantity"/></SUBSTITUTE_ITEM_QUANTITY>
    </SUB_COMP_TBL_ITEM>


    </xsl:when test="/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/BusinessComponentID/@schemeID != '' AND /SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/ID!='' AND /SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/BillOfMaterialsSubstituteComponentItem/ItemReference/ItemIdentification/ID='' AND /SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/Identification/ID=/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/BusinessComponentID@schemeID">

    <REV_ITEM>                    
                <REVISED_ITEM_NAME><xsl:value-of select="SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/Identification/ID"/></REVISED_ITEM_NAME>
    </REV_ITEML>
    <REV_COMP>      
                <COMPONENT_ITEM_NAME><xsl:value-of select="/SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/ID" /></COMPONENT_ITEM_NAME>                
    </REV_COMP>
    <SUB_COMP_TBL_ITEM>                   
                <REVISED_ITEM_NAME><!--empty--></REVISED_ITEM_NAME>                    
                <COMPONENT_ITEM_NAME><!--empty--></COMPONENT_ITEM_NAME>                   
                <SUBSTITUTE_COMPONENT_NAME><!--empty--></SUBSTITUTE_COMPONENT_NAME>
                <SUBSTITUTE_ITEM_QUANTITY><!--empty--></SUBSTITUTE_ITEM_QUANTITY>
    </SUB_COMP_TBL_ITEM>
    </xsl:when>

    <xsl:when OTHERPOSIBILITIES>
    </xsl:when>
<xsl:otherwise>
    <!-- EMPTY -->
 </xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

非常感谢任何帮助,创建和操作变量以指向正确的值需要我很多努力。

1 个答案:

答案 0 :(得分:0)

  1. 这将不会生成格式良好的XML,因为与根节点匹配的模板会生成包含三个文档元素的文档。

  2. 您的xslt格式不正确。关闭了你的第一个元素后,你需要打开另一个元素。

  3. 如果您发现自己正在编写for-each,然后尝试测试相关节点的某些特征,那么您可能应该使用apply-templates。

  4. 您的REVISED_ITEM_NAME个元素都不会被填充,因为没有SyncBillOfMaterialsList元素的子元素为SyncBillOfMaterialsListEBM

  5. 相对于上下文节点创建XPath表达式,而不是尝试根据position()从文档根目录中再次找到它。例如代替: /SyncBillOfMaterialsListEBM/DataArea/SyncBillOfMaterialsList[position()]/BillOfMaterialsComponentItem/Identification/BusinessComponentID/@schemeID,只需使用BillOfMaterialsComponentItem/Identification/BusinessComponentID/@schemeID即可。这更具可读性,position()的使用可能很脆弱,因为它不是指父节点内的节点的序号位置,而是指当前节点列表中的位置。如果您有一个XPath表达式明确选择其父级中的第五个子节点,position()将为1,而不是5。

  6. 不要尝试根据一次评估的条件集合创建整个树。看起来输出中的每个InputParameters元素具有相同的形状,但具有不同的值。因此,在SyncBillOfMaterialsList模板中,指定InputParameters树的形状一次,并有条件地填充其子元素。这将更易于维护和阅读。