将XML属性转换为XML元素

时间:2016-02-11 17:23:14

标签: xslt

我有以下XML输入:

<?xml version="1.0" encoding="utf-8"?>
<DOCUMENT>
  <EXTERNALFILES>
    <PRIMARYFILE FileName="C:\MSSB\POC\Exports\13f78581-1501-4dd0-8cf3-a5a300ba4083\121110-26-2001031100092.TIF" />
  </EXTERNALFILES>
  <OCRTEXTFILES />
  <DOCUMENTINDEX Name="A2iA_CheckAmount">3304.49</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="A2iA_CheckCAR">3304.49</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="A2iA_CheckCodeline_OnUs1">28557833</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="A2iA_CheckCodeline_Transit">031100092</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="A2iA_CheckDate">10-26-2001</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="A2iA_CheckLAR">3304.49</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="A2iA_CheckNumber">1211</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="A2iA_CheckPayeeName">BCBSD</DOCUMENTINDEX>
  <DOCUMENTINDEX Name="CheckPayerName">SOFTPRO NORTH AMERICA, INC.</DOCUMENTINDEX>
</DOCUMENT>

我需要将其转换为以下输出:

<?xml version="1.0" encoding="utf-8"?>
<DOCUMENT>
    <A2iA_CheckAmount>3304.49</A2iA_CheckAmount>
    <A2iA_CheckCAR>3304.49</A2iA_CheckCAR>
    <A2iA_CheckLAR>3304.49</A2iA_CheckLAR>
    <A2iA_CheckAccountNumber>28557833</A2iA_CheckAccountNumber>
    <A2iA_CheckRoutingNumber>031100092</A2iA_CheckRoutingNumber>
    <A2iA_CheckNumber>1211</A2iA_CheckNumber>
    <A2iA_CheckAmount>3304.49</A2iA_CheckAmount>
    <A2iA_CheckPayeeName>BCBSD</A2iA_CheckPayeeName>
    <CheckPayerName>SOFTPRO NORTH AMERICA, INC</CheckPayerName>
</DOCUMENT>

我正在使用以下XSLT代码:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
        <xsl:value-of select="concat('<DOCUMENT>')"/>
            <xsl:template match="/*">
            <xsl:for-each select="DOCUMENT/DOCUMENTINDEX/@*">
                <xsl:value-of select="concat('<', name(), '>', ., '</', name(), '>')"/>
            </xsl:for-each>
        <xsl:value-of select="concat('</DOCUMENT>')"/>
    </xsl:template>

我使用在线XSLT测试程序对此进行了测试,并且它产生了非描述性错误。我是XSLT Transformation的新手。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

实现这一目标的一种可能性是以下简单的( EDITED ):

<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="/DOCUMENT">
    <xsl:element name="Document">
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>
<xsl:template match="DOCUMENTINDEX">
    <xsl:element name="{@Name}">
        <xsl:value-of select="text()" />
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

它给出了输出( EDITED

<Document>
  <A2iA_CheckAmount>3304.49</A2iA_CheckAmount>
  <A2iA_CheckCAR>3304.49</A2iA_CheckCAR>
  <A2iA_CheckCodeline_OnUs1>28557833</A2iA_CheckCodeline_OnUs1>
  <A2iA_CheckCodeline_Transit>031100092</A2iA_CheckCodeline_Transit>
  <A2iA_CheckDate>10-26-2001</A2iA_CheckDate>
  <A2iA_CheckLAR>3304.49</A2iA_CheckLAR>
  <A2iA_CheckNumber>1211</A2iA_CheckNumber>
  <A2iA_CheckPayeeName>BCBSD</A2iA_CheckPayeeName>
  <CheckPayerName>SOFTPRO NORTH AMERICA, INC.</CheckPayerName>
</Document>