如何使用XSLT将此XML结构转换为另一个结构

时间:2014-05-22 09:21:48

标签: xml xslt

我有以下XML:

<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,我只能转换一个Header段但是有几个Header Segments我收到了一个错误。

这是我目前的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应该如何?

1 个答案:

答案 0 :(得分:0)

在.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>