将XML转换为可导入Access的XML

时间:2015-11-11 18:49:31

标签: xslt

我想使用XSLT将XML转换为可以导入到Access的输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<DatosGen>
    <Vendedor>
        <Nombre>Juan Garcia Sanchez</Nombre>
        <Codigo>VE144</Codigo>
    </Vendedor>
    <Fecha>30-abr-2015</Fecha>
</DatosGen>
<Pedidos>
    <Pedido>
        <Cabecera>
            <Numero>34</Numero>
            <Fecha>28-abr-2015</Fecha>
            <Cliente>
                <Nombre>Antonio Lopez Martin</Nombre>
                <Codigo>CL121</Codigo>
            </Cliente>
            <Portes>SI</Portes>
        </Cabecera>
        <Detalle>
            <Linea>
                <Articulo>Ar654</Articulo>
                <Cantidad>12</Cantidad>
                <Precio>2</Precio>
            </Linea>
            <Linea>
                <Articulo>Ar670</Articulo>
                <Cantidad>9</Cantidad>
                <Precio>1.21</Precio>
            </Linea>
            <Linea>
                <Articulo>Ar100</Articulo>
                <Cantidad>2</Cantidad>
                <Precio>3.45</Precio>
            </Linea>
            <Linea>
                <Articulo>Ar999</Articulo>
                <Cantidad>215</Cantidad>
                <Precio>12.24</Precio>
            </Linea>                
        </Detalle>
    </Pedido>
    <Pedido>
        <Cabecera>
            <Numero>35</Numero>
            <Fecha>29-abr-2015</Fecha>
            <Cliente>
                <Nombre>Manuel Enriquez Sousa</Nombre>
                <Codigo>CL160</Codigo>
            </Cliente>
            <Portes>NO</Portes>
        </Cabecera>
        <Detalle>
            <Linea>
                <Articulo>Ar003</Articulo>
                <Cantidad>10</Cantidad>
                <Precio>2.21</Precio>
            </Linea>
            <Linea>
                <Articulo>Ar005</Articulo>
                <Cantidad>8</Cantidad>
                <Precio>1.28</Precio>
            </Linea>
            <Linea>
                <Articulo>Ar100</Articulo>
                <Cantidad>5</Cantidad>
                <Precio>3.75</Precio>
            </Linea>
        </Detalle>
    </Pedido>
</Pedidos>
</Document>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot generated="2015-08-22T13:10:05" xmlns:od="urn:schemas-microsoft-com:officedata">
<importarxml>
    <Vendedor>Juan Garcia Sanchez</Vendedor>
    <idVendedor>VE144</idVendedor>
    <FechaFichero>30-abr-2015</FechaFichero>
    <Pedido>34</Pedido>
    <Fecha>28-abr-2015</Fecha>
    <Cliente>Antonio Lopez Martin</Cliente>
    <idCliente>CL121</idCliente>
    <Portes>SI</Portes>
    <Articulo>Ar100</Articulo>
    <Cantidad>5</Cantidad>
    <Precio>3.75</Precio>
</importarxml>
<importarxml>
    <Vendedor>Juan Garcia Sanchez</Vendedor>
    <idVendedor>VE144</idVendedor>
    <FechaFichero>30-abr-2015</FechaFichero>
    <Pedido>34</Pedido>
    <Fecha>28-abr-2015</Fecha>
    <Cliente>Antonio Lopez Martin</Cliente>
    <idCliente>CL121</idCliente>
    <Portes>SI</Portes>
    <Articulo>Ar005</Articulo>
    <Cantidad>8</Cantidad>
    <Precio>1.28</Precio>
</importarxml>
<importarxml>
    <Vendedor>Juan Garcia Sanchez</Vendedor>
    <idVendedor>VE144</idVendedor>
    <FechaFichero>30-abr-2015</FechaFichero>
    <Pedido>34</Pedido>
    <Fecha>28-abr-2015</Fecha>
    <Cliente>Antonio Lopez Martin</Cliente>
    <idCliente>CL121</idCliente>
    <Portes>SI</Portes>
    <Articulo>Ar003</Articulo>
    <Cantidad>10</Cantidad>
    <Precio>2.21</Precio>
</importarxml>
<importarxml>
    <Vendedor>Juan Garcia Sanchez</Vendedor>
    <idVendedor>VE144</idVendedor>
    <FechaFichero>30-abr-2015</FechaFichero>
    <Pedido>34</Pedido>
    <Fecha>28-abr-2015</Fecha>
    <Cliente>Antonio Lopez Martin</Cliente>
    <idCliente>CL121</idCliente>
    <Portes>SI</Portes>
    <Articulo>Ar999</Articulo>
    <Cantidad>215</Cantidad>
    <Precio>12.24</Precio>
</importarxml>
<importarxml>
    <Vendedor>Juan Garcia Sanchez</Vendedor>
    <idVendedor>VE144</idVendedor>
    <FechaFichero>30-abr-2015</FechaFichero>
    <Pedido>35</Pedido>
    <Fecha>29-abr-2015</Fecha>
    <Cliente>Manuel Enriquez Sousa</Cliente>
    <idCliente>CL160</idCliente>
    <Portes>No</Portes>
    <Articulo>Ar100</Articulo>
    <Cantidad>2</Cantidad>
    <Precio>3.45</Precio>
</importarxml>
<importarxml>
    <Vendedor>Juan Garcia Sanchez</Vendedor>
    <idVendedor>VE144</idVendedor>
    <FechaFichero>30-abr-2015</FechaFichero>
    <Pedido>35</Pedido>
    <Fecha>29-abr-2015</Fecha>
    <Cliente>Manuel Enriquez Sousa</Cliente>
    <idCliente>CL160</idCliente>
    <Portes>No</Portes>
    <Articulo>Ar670</Articulo>
    <Cantidad>9</Cantidad>
    <Precio>1.21</Precio>
</importarxml>
<importarxml>
    <Vendedor>Juan Garcia Sanchez</Vendedor>
    <idVendedor>VE144</idVendedor>
    <FechaFichero>30-abr-2015</FechaFichero>
    <Pedido>35</Pedido>
    <Fecha>29-abr-2015</Fecha>
    <Cliente>Manuel Enriquez Sousa</Cliente>
    <idCliente>CL160</idCliente>
    <Portes>No</Portes>
    <Articulo>Ar654</Articulo>
    <Cantidad>12</Cantidad>
    <Precio>2</Precio>
</importarxml>
</dataroot>

我的xslt在Pedido Fecha y Cliente中不起作用:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-08-22T13:10:05">
      <xsl:for-each select="//Document/Pedidos/Pedido/Detalle/Linea">
        <xsl:sort select="//Cliente/Nombre" order="descending" />
        <importarxml>
          <Vendedor>
            <xsl:value-of select="//Vendedor/Nombre" />
          </Vendedor>
          <idVendedor>
            <xsl:value-of select="//Vendedor/Codigo" />
          </idVendedor>
          <FechaFichero>
            <xsl:value-of select="//DatosGen/Fecha" />
          </FechaFichero>
          <Pedido>
            <xsl:value-of select="//Cabecera/Numero" />
          </Pedido>
          <Fecha>
            <xsl:value-of select="//Cabecera/Fecha" />
          </Fecha>
          <Cliente>
            <xsl:value-of select="//Cabecera/Cliente/Nombre" />
          </Cliente>
          <idCliente>
            <xsl:value-of select="//Cabecera/Cliente/Codigo" />
          </idCliente>
          <Portes>
            <xsl:value-of select="//Cabecera/Portes" />
          </Portes>
          <Articulo>
            <xsl:value-of select="Articulo" />
          </Articulo>
          <Cantidad>
            <xsl:value-of select="Cantidad" />
          </Cantidad>
          <Precio>
            <xsl:value-of select="Precio" />
          </Precio>
        </importarxml>
      </xsl:for-each>
    </dataroot>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

您的XPath引用不正确。

<xsl:for-each>内,除非您编写绝对路径(以<Linea>开头的路径),否则所有路径都相对于/个节点。这也是你的主要错误:绝对路径没有上下文,因此它们选择的节点不同于你想要的。

要使用相对路径访问供应商名称,首先需要上升四个级别:

<xsl:value-of select="../../../../DatosGen/Vendedor/Nombre" />

由于这很难遵循,因此使用ancestor轴更清楚:

<xsl:value-of select="ancestor::Document[1]/DatosGen/Vendedor/Nombre" />

但是当然输入中只有一个<DatosGen>,所以你也可以使用绝对路径:

<xsl:value-of select="/Document/DatosGen/Vendedor/Nombre" />

无论哪种方式,您需要不止一次,因此您可以创建和使用变量,如下所示:

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

  <xsl:template match="/Document">
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-08-22T13:10:05">
        <xsl:apply-templates select="Pedidos/Pedido/Detalle/Linea">
            <xsl:sort select="Cabecera/Cliente/Nombre" order="descending" />
        </xsl:apply-templates>
    </dataroot>
  </xsl:template>

  <xsl:template match="Linea">
    <xsl:variable name="DatosGen" select="ancestor::Document[1]/DatosGen" />
    <xsl:variable name="Cabecera" select="ancestor::Pedido[1]/Cabecera" />
    <importarxml>
      <Vendedor><xsl:value-of select="$DatosGen/Vendedor/Nombre" /></Vendedor>
      <idVendedor><xsl:value-of select="$DatosGen/Vendedor/Codigo" /></idVendedor>
      <FechaFichero><xsl:value-of select="$DatosGen/Fecha" /></FechaFichero>
      <Pedido><xsl:value-of select="$Cabecera/Numero" /></Pedido>
      <Fecha><xsl:value-of select="$Cabecera/Fecha" /></Fecha>
      <Cliente><xsl:value-of select="$Cabecera/Cliente/Nombre" /></Cliente>
      <idCliente><xsl:value-of select="$Cabecera/Cliente/Codigo" /></idCliente>
      <Portes><xsl:value-of select="$Cabecera/Portes" /></Portes>
      <Articulo><xsl:value-of select="Articulo" /></Articulo>
      <Cantidad><xsl:value-of select="Cantidad" /></Cantidad>
      <Precio><xsl:value-of select="Precio" /></Precio>
    </importarxml>
  </xsl:template>
</xsl:stylesheet>
相关问题