[英]How can I use XSLT to transform this XML structure to another structure
我有以下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段我會出錯。
這是我當前的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.