如何使用XSLT将此XML结构转换为另一个结构
我有以下XML:如何使用XSLT将此XML结构转换为另一个结构,xml,xslt,Xml,Xslt,我有以下XML: <Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Example.dtd" > <Sample1></Sample1> <Sample2></Sample2> <Child> <Sample4> </Sample4&
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Example.dtd" >
<Sample1></Sample1>
<Sample2></Sample2>
<Child>
<Sample4> </Sample4>
<Serv Test="T">
<Sample5></Sample5>
</Serv>
</Child>
</Header>
使用XSLT,我只能转换一个头段,但使用多个头段时出现错误
这是我当前的XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="ISO-8859-1" method="xml" indent="yes" doctype-public="-//Test.dtd" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这应该是输出:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Header
PUBLIC "-//Test.dtd XML//EN">
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Example.dtd" >
<Sample1></Sample1>
<Sample2></Sample2>
<Child>
<Sample4> </Sample4>
<Serv Test="T">
<Sample5></Sample5>
</Serv>
</Child>
</Header>
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Example.dtd" >
<Sample1></Sample1>
<Sample2></Sample2>
<Child>
<Sample4> </Sample4>
<Serv Test="T">
<Sample5></Sample5>
</Serv>
</Child>
</Header>
XSLT应该是什么样子?在.XSLT文件中尝试以下代码
<xsl:template match="Header">
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Example.dtd" >
<Sample1><xsl:value-of select="Sample1" /></Sample1>
<Sample2><xsl:value-of select="Sample2" /></Sample2>
<Child>
<Sample4><xsl:value-of select="Sample4" /></Sample4>
<Serv Test="T">
<Sample5><xsl:value-of select="Sample5" /></Sample5>
</Serv>
</Child>
</Header>
</xsl:template>
您的XSLT是身份转换-是否只想添加
DOCTYPE
并更改编码?不能有多个根元素?