XML转换

时间:2014-04-15 07:48:49

标签: xml xslt

我有一个复杂的XML,需要派生一个简单的xml。     我明白我需要一个xslt才能这样做。任何人都可以帮助我xslt?我对xslt没有任何线索。

`<?xml version="1.0"?>
<!DOCTYPE pkt SYSTEM "D:\packet-commented dtd.txt">
<pkt>
    <row name="ARENDEN" actn="M">
        <col name="ARNR" type="num" val="82844316"/>
        <col name="KUNDNR" type="num" val="290125"/>
        <col name="ANAMN1" type="str" len="36" val="FIRST TRAVEL COPENHAGEN"/>
        <col name="ANAMN2" type="str" len="36" val=""/>
        <col name="AADR1" type="str" len="36" val="K&#xD6;BMAGERGADE 55, 1SAL"/>
        <col name="AADR2" type="str" len="36" val=""/>
</row>
</pkt>`
<?xml version="1.0"?>
<!DOCTYPE pkt SYSTEM "D:\packet-commented dtd.txt">
<pkt>
    <row name="table 2" actn="M">
        <col name="attr1" type="num" val="82844316"/>
        <col name="attr2" type="num" val="290125"/>
        <col name="attr3" type="str" len="36" val="FIRST TRAVEL COPENHAGEN"/>

</row>
</pkt>`

my required xml is 

`<?xml version="1.0"?>
<table>
<col>"ARNR"</col>
<col>"KUNDNR"</col>
<col>"ANAMN1"</col>
<col>"attr1"</col>
<col>"attr2"</col>
<col>"attr3"</col>
</table>`

我做了一些xslt,下面是代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="w3.org/1999/XSL/Transform"; xmlns:xs="w3.org/2001/XMLSchema"; xmlns:fn="w3.org/2005/xpath-functions"; exclude-result-prefixes="xs fn">
  <xsl:output method="xml" encoding="UTF-8" byte-order-mark="no" indent="yes"/>
  <xsl:template match="/">
    <xsl:variable name="var1_pkt" as="node()?" select="pkt"/>
    <table>
      <xsl:attribute name="xsi:noNamespaceSchemaLocation" namespace="w3.org/2001/XMLSchema-instance"; select="'D:/XML/Output_xml.xsd'"/>
      <xsl:for-each select="$var1_pkt/row">
        <xsl:attribute name="name" select="fn:string(@name)"/>
      </xsl:for-each>
      <xsl:for-each select="$var1_pkt/row/col">
        <col>
          <xsl:sequence select="fn:string(@name)"/>
        </col>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

0 个答案:

没有答案