如何使用模板匹配实现嵌套XML转换?

时间:2014-06-04 05:10:46

标签: xml xslt for-loop xml-parsing nested

如何实现嵌套XML转换? 即必须转换多个层次标签时

我正在使用像这样匹配的模板,但是如果我尝试转换下一个直接元素标签,则子元素不会被转换

<xsl:for-each select="">
        <xsl:apply-templates select="" />
    </xsl:for-each>

    <xsl:template match="ONE">
        <ONE>

                <xsl:for-each select="*">
                    <xsl:attribute name="{name()}">
          <xsl:value-of select="text()" />
        </xsl:attribute>

                </xsl:for-each>
            </ONE>

    </xsl:template>

INPUT XML

<?xml version="1.0"?>
<ONE>
    <name>sam</name>
    <id>72622</id>
    <clgName>lourdes</objectName>
    <cgclgName>Lourdesboys</cgObjectName>
    <inputparameter />
    <outputparameter />
    <level>MIDLEVEL</level>
    <dataobjectview>
        <dataobject>
            <inherits />
            <isReverseEngineered>false</isReverseEngineered>
            <wsdlName />
            <NameSpace />
            <properties>
                <basic>
                    <XQuadrant>1</XQuadrant>
                    <YQuadrant>1</YQuadrant>
                    <annotations />
                    <imports />
                </basic>
            </properties>
        </dataobject>
    </dataobjectview>
</ONE>

OUTPUT XML必须是这样的

<?xml version="1.0" encoding="UTF-8"?>

<ONE name="Sam" id="72622" clgName="LourdesB" cgclgName="LourdesBoys"
    inputparameter="" outputparameter="" level="MIDLEVEL"
    <dataobjectview>
        <dataobject inherits=""
            isReverseEngineered="false" wsdlName="" NameSpace=""
        <property>
            <basic XQuadrant="1" YQuadrant="1" annotations="" imports="" />
        </property>
        />
    </dataobjectview>/>

1 个答案:

答案 0 :(得分:0)

很难回答,因为您的输入和输出都不是有效的XML文档。我猜你想做这样的事情:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="*">
    <xsl:copy>
        <xsl:for-each select="*[not(*)]">
            <xsl:attribute name="{name()}">
                <xsl:value-of select="." />
            </xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates select="*[*]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这会将没有子元素的元素转换为父元素的属性,然后递归继续执行

的子元素。

给出格式良好的输入,例如:

<?xml version="1.0" encoding="UTF-8"?>
<ONE>
   <name>sam</name>
   <id>72622</id>
   <clgName>lourdes</clgName>
   <cgclgName>Lourdesboys</cgclgName>
   <inputparameter/>
   <outputparameter/>
   <level>MIDLEVEL</level>
   <dataobjectview>
      <dataobject>
         <inherits/>
         <isReverseEngineered>false</isReverseEngineered>
         <wsdlName/>
         <NameSpace/>
         <properties>
            <basic>
               <XQuadrant>1</XQuadrant>
               <YQuadrant>1</YQuadrant>
               <annotations/>
               <imports/>
            </basic>
         </properties>
      </dataobject>
   </dataobjectview>
</ONE>

结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<ONE name="sam" id="72622" clgName="lourdes" cgclgName="Lourdesboys" inputparameter="" outputparameter="" level="MIDLEVEL">
   <dataobjectview>
      <dataobject inherits="" isReverseEngineered="false" wsdlName="" NameSpace="">
         <properties>
            <basic XQuadrant="1" YQuadrant="1" annotations="" imports=""/>
         </properties>
      </dataobject>
   </dataobjectview>
</ONE>