2014-01-30 155 views
0

您能否幫我使用xslt將xml轉換爲另一種格式?我有一個輸入XML需要被轉換爲另一種格式,但我嘗試使用XSLT,但徒勞無功。請爲我提供了一個示例代碼使用XSLT將一個XML轉換爲另一個格式

這裏是下面輸入XML:

<?xml version="1.0" encoding="UTF-8"?> 
<SyncApp xsi:schemaLocation="http://www.xyz.com/app/1_0 Application_1_0.xsd" xmlns="http://www.xyz.com/app/1_0" xmlns:env="http://www.xyz.com/group/common/1_0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<env:AppArea> 
    <env:Sender> 
     <env:LogicalID>String</env:LogicalID> 
    </env:Sender> 
    <env:Receiver> 
     <env:LogicalID>String</env:LogicalID> 
    </env:Receiver> 
    <env:CreationDateTime>2001-12-17T09:30:47Z</env:CreationDateTime> 
    <env:BODID>String</env:BODID> 
    <env:UserArea> 
     <env:BooleanValue name="String">true</env:BooleanValue> 
    </env:UserArea> 
</env:AppArea> 
<Data> 
    <Sync> 
     <env:ActionCriteria>String</env:ActionCriteria> 
     <env:UserArea> 
      <env:BooleanValue name="String">true</env:BooleanValue> 
     </env:UserArea> 
    </Sync> 
    <App> 
     <ID>1234</ID> 
     <NameShort>Test2</NameShort> 
     <NameLong>Test1</NameLong> 
     <Description>Test</Description> 
     <UserArea> 
      <env:BooleanValue name="String">true</env:BooleanValue> 
      <env:DateTimeValue name="String">2001-12-17T09:30:47Z</env:DateTimeValue> 
     </UserArea> 
    </App> 
</Data> 
</SyncApp> 

和輸出應該是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<SyncApp> 
<App Name="Test2" TypeNum="6" MDIDTest="1234"> 
    <AttrDef Name="MDID"> 
    <AttrValue Value="1234"/> 
    </AttrDef> 
    <AttrDef Name="LongName"> 
    <AttrValue Value="Test1"/> 
    </AttrDef> 
    <AttrDef Name="Description"> 
    <AttrValue Value="Test"/> 
    </AttrDef> 
</App> 
</SyncApp> 

請找到XSLT代碼:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output indent="yes" method="xml"/> 
<xsl:template match="/"> 
    <SynchFromXtoY> 
     <xsl:apply-templates select="SyncApp"/> 
    </SynchFromXtoY> 
</xsl:template> 
<xsl:template match="SyncApp"> 
    <xsl:apply-templates select="Data"/> 
</xsl:template> 
<xsl:template match="Data"> 
    <xsl:apply-templates select="App"/> 
</xsl:template> 
<xsl:template match="App"> 
<App> 
    <xsl:attribute name="Name"> 
     <xsl:value-of select="NameShort"/> 
    </xsl:attribute> 
    <xsl:attribute name="TypeNum"> 
     <xsl:value-of select="'6'"/> 
    </xsl:attribute>   
    <xsl:attribute name="MDIDTest"> 
     <xsl:value-of select="MDID"/> 
    </xsl:attribute> 
    <AttrDef> 
     <xsl:attribute name="Name"> 
     <xsl:value-of select="'MDID'"/> 
     </xsl:attribute> 
     <AttrValue> 
     <xsl:attribute name="Value"> 
     <xsl:value-of select="MDID"/> 
     </xsl:attribute> 
     </AttrValue> 
    </AttrDef> 
    <AttrDef> 
     <xsl:attribute name="Name"> 
     <xsl:value-of select="'LongName'"/> 
     </xsl:attribute> 
     <AttrValue> 
     <xsl:attribute name="Value"> 
     <xsl:value-of select="NameLong"/> 
     </xsl:attribute> 
     </AttrValue> 
    </AttrDef> 
    <AttrDef> 
     <xsl:attribute name="Name"> 
     <xsl:value-of select="'Description'"/> 
     </xsl:attribute> 
     <AttrValue> 
     <xsl:attribute name="Value"> 
     <xsl:value-of select="Description"/> 
     </xsl:attribute> 
     </AttrValue> 
    </AttrDef> 
    </App> 
</xsl:template> 
</xsl:stylesheet> 

提前致謝。

BR/Srinivas。

+0

到目前爲止您可以顯示您嘗試過的XSLT嗎?它可能是你非常接近解決方案,並且只需要進行一些調整就可以使其工作。謝謝! –

+0

嗨TIM, 請在帖子中找到我的代碼。 – user1179518

回答

2

你實際上並沒有遠離解決方案。你遇到的問題主要是命名空間。在你的XML,你有這樣的代碼......

<SyncApp xmlns="http://www.xyz.com/app/1_0" .... 

這是一個默認的命名空間,這意味着在你的XML不會有前綴(如「SyncApp」本身的任何元素的聲明,和「數據」和「App」)都屬於這個默認名稱空間。

但是,在您的XSLT中根本沒有提及這個名稱空間。這意味着當你這樣做時...

<xsl:template match="SyncApp"> 

這是尋找名爲「SyncApp」的元素,它沒有名稱空間。這與屬於名稱空間的名爲「SyncApp」的元素是不同的元素。名字空間實際上是元素名稱的一部分。

你需要做的是聲明命名空間在XSLT,像這樣(你使用可以是任何前綴,它是具有相匹配的URI)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:app="http://www.xyz.com/app/1_0" 

然後,在任何XPath表達式在那裏你指的元素在你的XML,你必須通過命名空間前綴

<xsl:template match="app:SyncApp"> 

前綴他們試試這個XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:app="http://www.xyz.com/app/1_0" 
    xmlns:env="http://www.xyz.com/group/common/1_0" 
    exclude-result-prefixes="app env"> 

<xsl:output indent="yes" method="xml"/> 

<xsl:template match="/"> 
    <SynchFromXtoY> 
     <xsl:apply-templates select="app:SyncApp"/> 
    </SynchFromXtoY> 
</xsl:template> 

<xsl:template match="app:SyncApp"> 
    <xsl:apply-templates select="app:Data"/> 
</xsl:template> 

<xsl:template match="app:Data"> 
    <xsl:apply-templates select="app:App"/> 
</xsl:template> 

<xsl:template match="app:App"> 
<App Name="{app:NameShort}" TypeNum="6" MDIDTest="{app:MDID}"> 
    <AttrDef Name="MDID"> 
     <AttrValue Value="{app:MDID}" /> 
    </AttrDef> 
    <AttrDef Name="LongName"> 
     <AttrValue Value="{app:NameLong}" /> 
    </AttrDef> 
    <AttrDef Name="Description"> 
     <AttrValue Value="{app:Description}" /> 
    </AttrDef> 
    </App> 
</xsl:template> 
</xsl:stylesheet> 

請注意在這裏使用Attribute Value Templates來簡化XSLT。而不是寫這個

<AttrValue> 
    <xsl:attribute name="Value"> 
     <xsl:value-of select="app:Description"/> 
    </xsl:attribute> 
    </AttrValue> 

的你可以這樣寫......

<AttrValue Value="{app:Description}" /> 

大括號指示要計算的表達式,而不是字面直接輸出。

+0

謝謝蒂姆..它的工作:) :) – user1179518

相關問題