0

我正在尝试将一些数据作为列而不是行返回,不确定是否可能?

所以我有以下xml:

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

<CashBalanceResult>
  <CashBalanceLine Id="2" Name="Name1" ParentId="1" level="2">
    <Balances>
      <genericDate date="2012-05-21" index="0">10.000000000000</genericDate>
      <genericDate date="2012-05-22" index="1">20.000000000000</genericDate>
      <genericDate date="2012-05-23" index="2">30.000000000000</genericDate>
      <genericDate date="2012-05-24" index="3">40.000000000000</genericDate>
      <genericDate date="2012-05-25" index="4">50.000000000000</genericDate>
    </Balances>
  </CashBalanceLine>
  <CashBalanceLine Id="170" Name="Name2" ParentId="2" level="3">
    <Balances>
      <genericDate date="2012-05-21" index="0">340.000000000000</genericDate>
      <genericDate date="2012-05-22" index="1">440.000000000000</genericDate>
      <genericDate date="2012-05-23" index="2">550.000000000000</genericDate>
      <genericDate date="2012-05-24" index="3">660.000000000000</genericDate>
      <genericDate date="2012-05-25" index="4">770.000000000000</genericDate>
    </Balances>
  </CashBalanceLine>
  <CashBalanceLine Id="179" Name="Name3" ParentId="170" level="4">
    <Balances>
      <genericDate date="2012-05-21" index="0">5.000000000000</genericDate>
      <genericDate date="2012-05-22" index="1">6.000000000000</genericDate>
      <genericDate date="2012-05-23" index="2">6.000000000000</genericDate>
      <genericDate date="2012-05-24" index="3">5.000000000000</genericDate>
      <genericDate date="2012-05-25" index="4">6.000000000000</genericDate>
    </Balances>
  </CashBalanceLine>
</CashBalanceResult>

然后我尝试使 genericdate 显示为 5 列:

name,2012-05-21,2012-05-22,2012-05-23,2012-05-24,2012-05-25
Name1,10.000000000000,20.000000000000,30.000000000000,40.000000000000,50.000000000000

Name1,10.000000000000,20.000000000000,30.000000000000,40.000000000000,50.000000000000 Name2,340.000000000000,440.000000000000,550.000000000000,660.000000000000,770.000000000000 Name3,5.000000000000,6.000000000000,6.000000000000,5.000000000000,6.000000000000

可能吗?非常感谢。

4

3 回答 3

0
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="CashBalanceResult">
    <xsl:text>Name,</xsl:text>
    <xsl:value-of select="*/Balances/genericDate[@index=0]/@date"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="*/Balances/genericDate[@index=1]/@date"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="*/Balances/genericDate[@index=2]/@date"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="*/Balances/genericDate[@index=3]/@date"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="*/Balances/genericDate[@index=4]/@date"/>
    <xsl:text>&#xA;</xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="CashBalanceLine">
    <xsl:value-of select="concat(@Name,',',
                  Balances/genericDate[@index=0],',',
                  Balances/genericDate[@index=1],',',
                  Balances/genericDate[@index=2],',',
                  Balances/genericDate[@index=3],',',
                  Balances/genericDate[@index=4]

                  )"/>

    <xsl:if test="following-sibling::CashBalanceLine">
      <xsl:text>&#xA;</xsl:text>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
于 2012-05-24T08:31:36.160 回答
0

这是一个 XSLT 2.0 选项...

XML 输入

<CashBalanceResult>
  <CashBalanceLine Id="2" Name="Name1" ParentId="1" level="2">
    <Balances>
      <genericDate date="2012-05-21" index="0">10.000000000000</genericDate>
      <genericDate date="2012-05-22" index="1">20.000000000000</genericDate>
      <genericDate date="2012-05-23" index="2">30.000000000000</genericDate>
      <genericDate date="2012-05-24" index="3">40.000000000000</genericDate>
      <genericDate date="2012-05-25" index="4">50.000000000000</genericDate>
    </Balances>
  </CashBalanceLine>
  <CashBalanceLine Id="170" Name="Name2" ParentId="2" level="3">
    <Balances>
      <genericDate date="2012-05-21" index="0">340.000000000000</genericDate>
      <genericDate date="2012-05-22" index="1">440.000000000000</genericDate>
      <genericDate date="2012-05-23" index="2">550.000000000000</genericDate>
      <genericDate date="2012-05-24" index="3">660.000000000000</genericDate>
      <genericDate date="2012-05-25" index="4">770.000000000000</genericDate>
    </Balances>
  </CashBalanceLine>
  <CashBalanceLine Id="179" Name="Name3" ParentId="170" level="4">
    <Balances>
      <genericDate date="2012-05-21" index="0">5.000000000000</genericDate>
      <genericDate date="2012-05-22" index="1">6.000000000000</genericDate>
      <genericDate date="2012-05-23" index="2">6.000000000000</genericDate>
      <genericDate date="2012-05-24" index="3">5.000000000000</genericDate>
      <genericDate date="2012-05-25" index="4">6.000000000000</genericDate>
    </Balances>
  </CashBalanceLine>
</CashBalanceResult>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="CashBalanceResult">
    <xsl:text>Name,</xsl:text>
    <xsl:value-of select="*[1]/Balances/*/@date" separator=","/>
    <xsl:text>&#xA;</xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="CashBalanceLine">
    <xsl:value-of select="concat(@Name,',')"/>
    <xsl:value-of select="Balances/*" separator=","/>
    <xsl:if test="following-sibling::CashBalanceLine">
      <xsl:text>&#xA;</xsl:text>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

输出

Name,2012-05-21,2012-05-22,2012-05-23,2012-05-24,2012-05-25
Name1,10.000000000000,20.000000000000,30.000000000000,40.000000000000,50.000000000000
Name2,340.000000000000,440.000000000000,550.000000000000,660.000000000000,770.000000000000
Name3,5.000000000000,6.000000000000,6.000000000000,5.000000000000,6.000000000000
于 2012-05-23T15:27:49.260 回答
-1

如果您在示例中的每一行中有固定数量的genericDate元素,那么在 XSLT 1.0 中实际上有一种相当简单的通用方法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*" />

  <xsl:template match="CashBalanceResult">
    <xsl:text>name</xsl:text>
    <xsl:apply-templates select="CashBalanceLine[1]" mode="titles" />
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="genericDate" mode="titles">
    <xsl:text>,</xsl:text>
    <xsl:value-of select="@date" />
  </xsl:template>

  <xsl:template match="CashBalanceLine">
    <xsl:value-of select="@Name" />
    <xsl:apply-templates />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="genericDate">
    <xsl:text>,</xsl:text>
    <xsl:value-of select="." />
  </xsl:template>
</xsl:stylesheet>
于 2012-05-24T08:48:40.723 回答