通过XSLT根据条件更改xml属性

时间:2012-06-18 08:39:59

标签: xslt

任何人都可以帮助我通过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的属性,我没有尝试更新我想要更新的DrawingSheet名称。

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

提前致谢。

3 个答案:

答案 0 :(得分:1)

最后跟随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 Durkin

答案 1 :(得分: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>

答案 2 :(得分:0)

帕拉格,祝贺自己的好回答!

虽然这可能不是更好,但为了您的兴趣,这是另一种解决方案。在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>