如何使用XSLT将XML 1转换为XML 2?我是XSLT的新手。
XML 1 - 这是第一个XML
<?xml version="1.0" encoding="utf-8"?>
<root type="array">
<item type="object">
<a:item xmlns:a="item" item="@id" type="string">_:genid1</a:item>
<a:item xmlns:a="item" item="@type" type="array">
<item type="string">http://www.w3.org/2000/01/rdf-schema#Datatype</item>
</a:item>
<a:item xmlns:a="item" item="http://www.w3.org/2002/07/owl#oneOf" type="array">
<item type="object">
<a:item xmlns:a="item" item="@list" type="array">
<item type="object">
<a:item xmlns:a="item" item="@value" type="string">L</a:item>
</item>
<item type="object">
<a:item xmlns:a="item" item="@value" type="string">Q</a:item>
</item>
<item type="object">
<a:item xmlns:a="item" item="@value" type="string">R</a:item>
</item>
</a:item> <!-- added by edit -->
</item>
</a:item>
</item>
</root>
XML 2 - 这是我将第一个XML转换为。
所需的第二个XML<?xml version="1.0" encoding="utf-8"?>
<root type="array">
<persons>
<person person_id = "_genid1"></person>
<type>
http//www.w3.org/2000/01/rdf-schema#Datatype
</type>
<oneofs>
<oneof>
L|Q|R
</oneof>
</oneofs>
</persons>
</root>
答案 0 :(得分:0)
XSLT-2.0解决方案就是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="item">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="@*" />
<persons>
<person person_id="{substring-after(item/a:item[@type='string'],'_')}" />
<type><xsl:value-of select="item/a:item/item[@type='string']" /></type>
<oneofs>
<oneof><xsl:value-of select="item/a:item[@item='http://www.w3.org/2002/07/owl#oneOf']/item[@type='object']/a:item/item/a:item" separator="|" /></oneof>
</oneofs>
</persons>
<xsl:value-of select="' '" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出为:
<?xml version="1.0" encoding="UTF-8"?>
<root type="array">
<persons xmlns:a="item">
<person person_id=":genid1"/>
<type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
<oneofs>
<oneof>L|Q|R</oneof>
</oneofs>
</persons>
</root>