XSLT合并/组合节点

3
抱歉,也许这个问题已经被提出了,但我找不到任何可以帮助我的东西,可能是由于我对XSLT的知识不足。
我有以下XML:
<xml> 
  <EstadoRespuesta>   
    <Error>0</Error>   
    <Estatus>OK</Estatus> 
  </EstadoRespuesta> 
  <NewDataSet>   
    <Table1>
      <Elemento>Cuenta</Elemento>
      <Valor>XZY07633</Valor>   
    </Table1>   
    <Table1>
      <Elemento>Fecha del Balance</Elemento>
      <Valor>2016-Nov-18 19:15</Valor>   
    </Table1>   
    <Table1>
      <Elemento>Balance</Elemento>
      <Valor>60.7</Valor>   
    </Table1>   
    <Table1>
      <Elemento>Lectura</Elemento>
      <Valor>2,152.4</Valor>   
    </Table1>   
    <Table1>
      <Elemento>Suspensión al llegar a</Elemento>
      <Valor>2,213.1</Valor>   
    </Table1>   
    <Table1>
      <Elemento>Fecha aproximada de corte*</Elemento>
      <Valor>2017-Jan-04 15:37</Valor>   
    </Table1> 
  </NewDataSet> 
</xml>

我希望您能将其转化为以下内容:

<xml>
<EstadoRespuesta>
  <Error>0</Error>
  <Estatus>OK</Estatus>
</EstadoRespuesta>
<NewDataSet>
  <Table1>
    <Cuenta>XZY07633</Cuenta>
  </Table1>
  <Table1>
    <FechadelBalance>2016-Nov-18 19:15</FechadelBalance>
  </Table1>
  <Table1>
    <Balance>60.7</Balance>
  </Table1>
  <Table1>
    <Lectura>2,152.4</Lectura>
  </Table1>
  <Table1>
    <Suspensiónalllegara>2,213.1</Suspensiónalllegara>
  </Table1>
  <Table1>
    <Fechaaproximadadecorte>2017-Jan-04 15:37</Fechaaproximadadecorte>
  </Table1>
</NewDataSet>
</xml>

我使用了这个:
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <records>
    <xsl:apply-templates/>
  </records>
 </xsl:template>

 <xsl:template match="NewDataSet">
  <record>
    <xsl:apply-templates/>
  </record>
 </xsl:template>

<xsl:template match="Table1">
  <record>
    <xsl:apply-templates/>
  </record>
 </xsl:template>

 <xsl:template match="Elemento">
  <xsl:attribute name="local-name(.)">
    <xsl:value-of select="Valor"/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

但我并不排斥获取我需要的东西。有人能给我一些建议/帮助吗?提前感谢。

2个回答

1
尝试这个方法:

XSLT 1.0

<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:strip-space elements="*"/>

<xsl:variable name="valid-chars">abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.</xsl:variable>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Table1">
    <xsl:copy>
        <xsl:element name="{translate(Elemento, translate(Elemento, $valid-chars, ''), '')}">
             <xsl:value-of select="Valor"/>
        </xsl:element>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

注意:

  1. 您需要将想要保留的字符(例如ó)添加到有效字符列表中。

  2. 即使过滤了无效字符,输入仍可能是无效的XML元素名称(例如,如果以数字开头)。


1
一种 XSLT 1.0 的解决方案如下:
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/xml">
  <xml>
    <xsl:copy-of select="EstadoRespuesta" />
    <NewDataSet1>
      <xsl:apply-templates select="NewDataSet" />
    </NewDataSet1>
  </xml>
 </xsl:template>

 <xsl:template match="Table1">
  <Table1>
    <xsl:element name="{translate(Elemento,' *ó','')}">
      <xsl:value-of select="Valor" />
    </xsl:element>
  </Table1>
 </xsl:template>
</xsl:stylesheet>

它的结果是。
<xml>
    <EstadoRespuesta>
        <Error>0</Error>
        <Estatus>OK</Estatus>
    </EstadoRespuesta>
    <NewDataSet1>
        <Table1>
            <Cuenta>XZY07633</Cuenta>
        </Table1>
        <Table1>
            <FechadelBalance>2016-Nov-18 19:15</FechadelBalance>
        </Table1>
        <Table1>
            <Balance>60.7</Balance>
        </Table1>
        <Table1>
            <Lectura>2,152.4</Lectura>
        </Table1>
        <Table1>
            <Suspensinalllegara>2,213.1</Suspensinalllegara>
        </Table1>
        <Table1>
            <Fechaaproximadadecorte>2017-Jan-04 15:37</Fechaaproximadadecorte>
        </Table1>
    </NewDataSet1>
</xml>

然而,在XSLT文件中,您必须将所有非QName字符添加到translate表达式的第二个参数中。

translate(Elemento,' *ó','')


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接