0

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

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

4

1 回答 1

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

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

  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这将更易于维护和阅读。

于 2013-07-15T11:09:54.480 回答