2

任何人都可以帮助通过 XSLT 转换我的 xml。我是这个 xslt 的新手,无法让它工作。

这是示例输入 xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TCXML xmlns="http://www.tcxml.org/Schemas/TCXMLSchema">
  <BOMLine bl_appearance_path_node="" bl_attachments="#id13 #id15 #id20 #id21 #id22 #id3" bl_bomview="" bl_bomview_rev="" bl_gde_bomview_rev="" bl_item="#id7" bl_real_occurrence="" bl_revision="#id8" elemId="id61">

    <GSIdentity elemId="id2" label="_RnrSUKuM1CaiD"/>

</BOMLine>
 <DrawingSheet creation_date="2012-06-05T09:27:19Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id69" format_used="BINARY" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:27:20Z" license_list="" object_desc="" object_name="Sheet 1" owning_organization="">

    <GSIdentity elemId="id24" label="RoL9sTu6BT4jNA"/>

</DrawingSheet>
  <DrawingSheet creation_date="2012-06-05T09:28:44Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id70" format_used="BINARY" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:28:45Z" license_list="" object_desc="" object_name="Sheet 1" owning_organization="">

    <GSIdentity elemId="id25" label="RoO9sTvgBT4jNA"/>

</DrawingSheet>
  <DrawingSheet creation_date="2012-06-05T09:29:29Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id71" format_used="BINARY" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:29:29Z" license_list="" object_desc="" object_name="Sheet 1" owning_organization="">

    <GSIdentity elemId="id26" label="RoO9sTfVBT4jNA"/>

</DrawingSheet>
 <IMAN_Drawing elemId="id78" owning_site="#id4" primary_object="#id21" secondary_object="#id25" user_data="">

    <GSIdentity elemId="id50" label="R8D9sTvgBT4jNA"/>

</IMAN_Drawing>
  <IMAN_Drawing elemId="id79" owning_site="#id4" primary_object="#id22" secondary_object="#id26" user_data="">

    <GSIdentity elemId="id56" label="R8D9sTfVBT4jNA"/>

</IMAN_Drawing>
  <IMAN_Drawing elemId="id80" owning_site="#id4" primary_object="#id20" secondary_object="#id24" user_data="">

    <GSIdentity elemId="id44" label="R8A9sTu6BT4jNA"/>

</IMAN_Drawing>
<UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id117" format_used="PART" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:25:34Z" license_list="" object_desc="dwg3" object_name="dwg3" owning_organization="">

    <GSIdentity elemId="id21" label="RXM9sPifBT4jNA"/>

</UGPART>
  <UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id118" format_used="PART" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:25:33Z" license_list="" object_desc="dwg1" object_name="dwg1" owning_organization="">

    <GSIdentity elemId="id20" label="R3K9sPifBT4jNA"/>

</UGPART>
  <UGPART creation_date="2012-06-05T09:25:28Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id119" format_used="PART" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:25:34Z" license_list="" object_desc="dwg2" object_name="dwg2" owning_organization="">

    <GSIdentity elemId="id22" label="xuO9sPifBT4jNA"/>

</UGPART>
</TCXML>

在这个 xml 文件中有 3 个 IMAN_Drawing 、3 个图纸和 3 个 UGPART 标签。例如 ,

<IMAN_Drawing elemId="id78" owning_site="#id4" primary_object="#id21" secondary_object="#id25" user_data="">

    <GSIdentity elemId="id50" label="R8D9sTvgBT4jNA"/>

</IMAN_Drawing>

所以我想复制UGPART(在这种情况下为primary_object id21)object_name 并附加到DrawingSheet(在这种情况下为secondary_object id25)object_name。所以最终 DrawingSheet 名称将是 UGPART 的 object_name _ DrawingSheet 的 object_name。对于这种情况,DrawingSheet object_name 将更新为 dwg3_Sheet 1

所以最后我想为我的 input.xml 输出 xml,如下所示。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TCXML xmlns="http://www.tcxml.org/Schemas/TCXMLSchema">
  <BOMLine bl_appearance_path_node="" bl_attachments="#id13 #id15 #id20 #id21 #id22 #id3" bl_bomview="" bl_bomview_rev="" bl_gde_bomview_rev="" bl_item="#id7" bl_real_occurrence="" bl_revision="#id8" elemId="id61">

    <GSIdentity elemId="id2" label="_RnrSUKuM1CaiD"/>

</BOMLine>
 <DrawingSheet creation_date="2012-06-05T09:27:19Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id69" format_used="BINARY" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:27:20Z" license_list="" object_desc="" object_name="dwg1_Sheet 1" owning_organization="">

    <GSIdentity elemId="id24" label="RoL9sTu6BT4jNA"/>

</DrawingSheet>
  <DrawingSheet creation_date="2012-06-05T09:28:44Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id70" format_used="BINARY" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:28:45Z" license_list="" object_desc="" object_name="dwg3_Sheet 1" owning_organization="">

    <GSIdentity elemId="id25" label="RoO9sTvgBT4jNA"/>

</DrawingSheet>
  <DrawingSheet creation_date="2012-06-05T09:29:29Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id71" format_used="BINARY" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:29:29Z" license_list="" object_desc="" object_name="dwg2_Sheet 1" owning_organization="">

    <GSIdentity elemId="id26" label="RoO9sTfVBT4jNA"/>

</DrawingSheet>
 <IMAN_Drawing elemId="id78" owning_site="#id4" primary_object="#id21" secondary_object="#id25" user_data="">

    <GSIdentity elemId="id50" label="R8D9sTvgBT4jNA"/>

</IMAN_Drawing>
  <IMAN_Drawing elemId="id79" owning_site="#id4" primary_object="#id22" secondary_object="#id26" user_data="">

    <GSIdentity elemId="id56" label="R8D9sTfVBT4jNA"/>

</IMAN_Drawing>
  <IMAN_Drawing elemId="id80" owning_site="#id4" primary_object="#id20" secondary_object="#id24" user_data="">

    <GSIdentity elemId="id44" label="R8A9sTu6BT4jNA"/>

</IMAN_Drawing>
<UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id117" format_used="PART" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:25:34Z" license_list="" object_desc="dwg3" object_name="dwg3" owning_organization="">

    <GSIdentity elemId="id21" label="RXM9sPifBT4jNA"/>

</UGPART>
  <UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id118" format_used="PART" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:25:33Z" license_list="" object_desc="dwg1" object_name="dwg1" owning_organization="">

    <GSIdentity elemId="id20" label="R3K9sPifBT4jNA"/>

</UGPART>
  <UGPART creation_date="2012-06-05T09:25:28Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id119" format_used="PART" gov_classification="" highest_rev_prop="1" ip_classification="" keep_limit_prop="3" last_mod_date="2012-06-05T09:25:34Z" license_list="" object_desc="dwg2" object_name="dwg2" owning_organization="">

    <GSIdentity elemId="id22" label="xuO9sPifBT4jNA"/>

</UGPART>
</TCXML>

我正在寻找 xslt 1.0 解决方案。

这是我尝试使用的 xslt(我试图通过参数传递值来更新 UGPART 属性,但它是空值),但不知何故它不起作用。由于我无法更新 UGPART 的属性,因此我没有尝试更新我想要更新的绘图表名称。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:plm="http://www.tcxml.org/Schemas/TCXMLSchema" version="1.0">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/plm:TCXML/plm:IMAN_Drawing">  
<xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:for-each select="/plm:TCXML/plm:IMAN_Drawing">
        <xsl:variable name="currentSecObjectId" select="@secondary_object"/>
        <xsl:variable name="RefSecObjectId" select="substring($currentSecObjectId,2)"/>
        <xsl:variable name="currentPrimaryObjectId" select="/plm:TCXML/plm:IMAN_Drawing/@primary_object"/>
        <xsl:variable name="RefPrimaryObjectId" select="substring($currentPrimaryObjectId,2)"/>
        <xsl:call-template name="UGPart">
            <xsl:with-param name="PriId" select="$RefPrimaryObjectId"/>
        </xsl:call-template>
    </xsl:for-each>

    <xsl:apply-templates/>  

</xsl:copy> 
</xsl:template> 
    <xsl:template match="/plm:TCXML/plm:UGPART" name="UGPart">
    <xsl:param name="PriId"/>
    <xsl:copy>
    <xsl:if test="@object_name="'Test'">
        <xsl:attribute name="owning_organization">
            <xsl:value-of select="$PriId"/>
        </xsl:attribute>        
    </xsl:if>
      <xsl:copy-of select="@*[not(name()='owning_organization')]" /> 
    <xsl:apply-templates /> 
    </xsl:copy>

</xsl:template>

提前致谢。

4

3 回答 3

1

Finnaly followign xslt 为我的问题工作:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:plm="http://www.tcxml.org/Schemas/TCXMLSchema" version="1.0">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <!-- This is general rule to copy every thing that we dont specify condition-->
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="plm:IMAN_Drawing">
    <xsl:variable name="Pri_id" select="substring(@primary_object,2)" />
    <xsl:variable name="Sec_id" select="substring(@secondary_object,2)" />
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy> 


        <xsl:for-each select="../plm:UGPART[plm:GSIdentity/@elemId=$Pri_id]">
            <xsl:variable name="UGpartName" select="@object_name"/>
            <xsl:copy>
            <xsl:apply-templates select="@*"/>                      
            <xsl:apply-templates select="node()"/>
            </xsl:copy>
            <xsl:call-template name="DrawingSheet">
                <xsl:with-param name="Sec_ID_For_Drawing" select="$Sec_id"/>
                <xsl:with-param name="UGPART_Name" select="$UGpartName"/>   
            </xsl:call-template>
        </xsl:for-each>


</xsl:template>

<xsl:template match="plm:UGPART"/>
<xsl:template match="plm:DrawingSheet"/>
<xsl:template match="plm:DrawingSheet" name ="DrawingSheet">
<xsl:param name="Sec_ID_For_Drawing"/>
<xsl:param name="UGPART_Name"/>
<xsl:for-each select="../plm:DrawingSheet[plm:GSIdentity/@elemId=$Sec_ID_For_Drawing]">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:variable name="DarwingObjectName" select="@object_name"/>
        <xsl:attribute name="object_name">
            <xsl:value-of select="concat($UGPART_Name,'_',$DarwingObjectName)"/>
        </xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet> 

感谢所有帮助过我的人。谢谢肖恩。B杜尔金

于 2012-06-19T03:30:05.980 回答
0

我会使用以下模式,非常轻巧干净:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <!-- identity template to copy input xml as is --> 
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <!-- specific template to update just DrawingSheet -->
    <xsl:template match="DrawingSheet">

        <!-- get UPGART id from IMAN_Drawing -->
        <!-- get UPGART object_name -->
        <!-- update Drawing Sheet obejct_name -->

    </xsl:template>

</xsl:stylesheet>
于 2012-06-18T11:44:32.977 回答
0

Parag,恭喜您获得了良好的自我回答!

尽管这可能不会更好,但出于您的兴趣,这里有另一种不同角度的解决方案。在 Empo 的解决方案的基础上,它采用功能视图,提出问题,即作为输入函数表示的输出是什么,而不是过程(for-each、call-template 等)视图。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:plm="http://www.tcxml.org/Schemas/TCXMLSchema" version="1.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="node()|@*">
 <xsl:copy>
  <xsl:apply-templates select="node()|@*"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="plm:DrawingSheet">
 <xsl:variable name="Sec_Id" select="plm:GSIdentity/@elemId" />
 <xsl:variable name="Pri_id" select="
    substring(
       ../plm:IMAN_Drawing[$Sec_Id=substring(@secondary_object,2)]
       /@primary_object,2)" />
 <xsl:copy>
  <xsl:apply-templates select="@*"/>
   <xsl:attribute name="object_name">
     <xsl:value-of select="concat(
        ../plm:UGPART[plm:GSIdentity/@elemId=$Pri_id]/@object_name,
       '_',
       @object_name)" /> 
   </xsl:attribute>
  <xsl:apply-templates select="node()"/>
 </xsl:copy>  
</xsl:template>

</xsl:stylesheet>
于 2012-06-19T05:22:50.063 回答