您能否幫我使用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。
到目前爲止您可以顯示您嘗試過的XSLT嗎?它可能是你非常接近解決方案,並且只需要進行一些調整就可以使其工作。謝謝! –
嗨TIM, 請在帖子中找到我的代碼。 – user1179518