0

我在 XSLT 下编写以实现我的报告要求所需的翻译。

虽然,输入和输出 XML 是不言自明的,但简而言之,要求是通过合并由公共属性相关的不同节点对,从现有 XML 构建新的 XML。

输入文件

<?xml version="1.0" encoding="utf-8"?>
<dataset  xmlns="http://developer.cognos.com/schemas/xmldata/1/"  xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<crosstab>
<values>
  <value row="R1" col="C1">111</value>
  <value row="R1" col="C2">222</value>
  <value row="R1" col="C3">333</value>
  <value row="R1" col="C4">444</value>
  <value row="R1" col="C5">555</value>
  <value row="R1" col="C6">666</value>
  <value row="R1" col="C7">777</value>
</values>
<corner>
  <caption>Number of Employees</caption>
</corner>
<columns>
  <colEdge>
    <caption>2010</caption>
    <colEdge id="C1">
      <caption>October</caption>
    </colEdge>
    <colEdge id="C2">
      <caption>November</caption>
    </colEdge>
    <colEdge id="C3">
      <caption>December</caption>
    </colEdge>
  </colEdge>
  <colEdge>
    <caption>2011</caption>
    <colEdge id="C4">
      <caption>January</caption>
    </colEdge>
    <colEdge id="C5">
      <caption>February</caption>
    </colEdge>
    <colEdge id="C6">
      <caption>March</caption>
    </colEdge>
    <colEdge id="C7">
      <caption>April</caption>
    </colEdge>
  </colEdge>
</columns>
<rows>
</rows>

输出文件

<?xml version="1.0" encoding="utf-8"?>
<dataset xmlns:c="http://developer.cognos.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<crosstab>
<values>
  <value col="October 2010">111</value>
  <value col="November 2010">222</value>
  <value col="December 2010">333</value>
  <value col="January 2011">444</value>
  <value col="February 2011">555</value>
  <value col="March 2011">666</value>
  <value col="April 2011">777</value>
</values>
<corner>
  <caption>Number of Employees</caption>
</corner>
</crosstab>
</dataset>
4

1 回答 1

0

下面是我用来实现所需翻译的 XSLT。

XSLT 文件

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:c="http://developer.cognos.com/schemas/xmldata/1/"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Navigating crosstab node -->
<xsl:template match="c:crosstab">
<dataset>
  <crosstab>
    <xsl:apply-templates select="c:values" />
    <xsl:apply-templates select="c:corner" />
  </crosstab>
</dataset>
</xsl:template>
<!-- Navigating values node -->
<xsl:template match="c:values">
<values>
  <xsl:apply-templates select="c:value" />
</values>
</xsl:template>
<!-- Reading attributes of the value nodes -->
<xsl:template match="c:value">
<value>
  <xsl:apply-templates select="@col"/>
  <xsl:apply-templates/>
</value>
</xsl:template>
<!-- Reading Month and Year nodes for the selected attribute -->
<xsl:template match="@col">
 <xsl:attribute name="{name()}">
  <xsl:variable name="colAttribute" select="."/>
  <xsl:variable name="year" select="//c:colEdge[@id=$colAttribute[1]]/../*[1]/child::node()[1]"/>
  <xsl:variable name="month" select="//c:colEdge[@id=$colAttribute[1]]/child::node()[1]/child::node()[1]"/>
  <xsl:value-of select="concat($month,' ',$year)" />
</xsl:attribute>
</xsl:template>
<!-- Reading attributes of the corner node -->
<xsl:template match="c:corner">
<corner>
  <caption>
    <xsl:value-of select="." />
  </caption>
</corner>
</xsl:template>
</xsl:stylesheet>
于 2012-11-02T13:24:36.470 回答