使用XSLT将一种XML转换为另一种格式
你能帮我用xslt把xml转换成另一种格式吗?我有一个需要转换为另一种格式的输入XML,但是我尝试使用XSLT,但没有成功。请给我一个示例代码 以下是输入xml:使用XSLT将一种XML转换为另一种格式,xslt,Xslt,你能帮我用xslt把xml转换成另一种格式吗?我有一个需要转换为另一种格式的输入XML,但是我尝试使用XSLT,但没有成功。请给我一个示例代码 以下是输入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:en
<?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>
一串
一串
2001-12-17T09:30:47Z
一串
真的
一串
真的
1234
测试2
测试1
试验
真的
2001-12-17T09:30:47Z
输出应如下所示:
<?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。您实际上离解决方案并不遥远。您遇到的问题主要是名称空间。在XML中有以下代码
<SyncApp xmlns="http://www.xyz.com/app/1_0" ....
是否可以显示您迄今为止尝试过的XSLT?可能你已经非常接近一个解决方案了,只需要调整一下就可以了。谢谢嗨,蒂姆,请在帖子中找到我的代码。
<xsl:template match="SyncApp">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:app="http://www.xyz.com/app/1_0"
<xsl:template match="app:SyncApp">
<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>
<AttrValue>
<xsl:attribute name="Value">
<xsl:value-of select="app:Description"/>
</xsl:attribute>
</AttrValue>
<AttrValue Value="{app:Description}" />