2012-06-18 49 views
2

任何人都可以通過XSLT幫助我轉換我的xml。 我是這個xslt的新手,無法使它工作。根據條件通過XSLT更改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="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的OBJECT_NAME(在這種情況下secondary_object ID25)。所以最後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> 

在此先感謝。

+0

歡迎來到SO。人們很可能會幫助你,但不能完成整個任務。告訴我們你試過的東西。 – Utkanos

+0

這裏有些不對勁。你說你想讓DrawingSheet/@ object_name成爲UGPART節點的一個功能,但是兩者之間沒有明確的聯繫。 –

+0

@Parag,可以編輯輸入和輸出文檔,將它們剪下來以顯示說明問題所需的絕對最小值。樣品不必要的複雜,所以我們不能看到樹木。 –

回答

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

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

謝謝肖恩,這是非常好的解決方案。我會用這個。 – parag