1

我正在做一个项目并且遇到了问题。我最近确定无法更改变量值,并且似乎无法找到解决此问题的方法。我需要创建通用的代码,但我无法做到这一点。

我的 XSLT 代码......

                   <tracklist>
                        <xsl:for-each select = "VinylRecord/Tracks/Song">

                            <xsl:choose>

                                <xsl:when test = "@Track = 1">
                                    <xsl:element name="side{@Side}">

                                    <xsl:variable name = "d" select = "@Track"/>

                                        <xsl:for-each select = "../Song">
                                            <xsl:choose>
                                                <xsl:when test = "@Side = ../Song/following:: @Side">

                                                    <track>

                                                        <xsl:attribute name = "no">
                                                            <xsl:value-of select = "@Track" />
                                                        </xsl:attribute>

                                                        <xsl:attribute name = "runtime">
                                                            <xsl:value-of select = "Length" />
                                                        </xsl:attribute>

                                                        <xsl:value-of select = "text()" />

                                                    </track>

                                                </xsl:when>                                               
                                            </xsl:choose>
                                        </xsl:for-each>


                                    </xsl:element>
                                </xsl:when>

                            </xsl:choose>

                        </xsl:for-each>
                    </tracklist>

XML 文档…………

    <Tracks>
        <Song Side="A" Track="1">
            The Nerve
            <Length>2.58</Length>
        </Song>
        <Song Side="A" Track="2">
            Backfire
            <Length>3.22</Length>
        </Song> 
        <Song Side="A" Track="3">
            Clipping
            <Length>4.05</Length>
        </Song>
        <Song Side="A" Track="4">
            Spotlight
            <Length>3.21</Length>
        </Song>
        <Song Side="A" Track="5">
            No Response
            <Length>4.01</Length>
        </Song>
        <Song Side="A" Track="6">
            Pins and Needles
            <Length>4.05</Length>
        </Song>
        <Song Side="B" Track="1">
            Goodbye
            <Length>4.09</Length>
        </Song>
        <Song Side="B" Track="2">
            Odds
            <Length>3.01</Length>
        </Song>
        <Song Side="B" Track="3">
            Electrify
            <Length>3.49</Length>
        </Song>
        <Song Side="B" Track="4">
            Armistice
            <Length>3.54</Length>
        </Song>
        <Song Side="B" Track="5">
            Lost Year
            <Length>3.13</Length>
        </Song>
        <Song Side="B" Track="6">
            Burden
            <Length>9.06</Length>
        </Song>
    </Tracks>

...

任何提示将非常感谢

预期结果 ...............

这只是上面显示的整个程序的一个片段,但结果应该显示为:

        <tracklist>
            <sideA>
                <track no="1" runtime="2.58">The Nerve</track>
                <track no="2" runtime="3.22">Backfire</track>
                <track no="3" runtime="4.05">Clipping</track>
                <track no="4" runtime="3.21">Spotlight</track>
                <track no="5" runtime="4.01">No Response</track>
                <track no="6" runtime="4.05">Pins and Needles</track>
            </sideA>
            <sideB>
                <track no="1" runtime="4.09">Goodbye</track>
                <track no="2" runtime="3.01">Odds</track>
                <track no="3" runtime="3.49">Electrify</track>
                <track no="4" runtime="3.54">Armistice</track>
                <track no="5" runtime="3.13">Lost Year</track>
                <track no="6" runtime="9.06">Burden</track>
            </sideB>
        </tracklist>
4

3 回答 3

0

“这不是一个错误,而是一个特性”:XSLT 变量被设计为不可更改。实际上它们可以被命名为常量。解决这个问题很困难,可以使用参数来完成。在大多数情况下,如果您尝试使用 XSLT 编程尝试,则没有必要这样做,其中程序是通过模板由数据驱动的。诸如“for-each”之类的东西是不真正适合 XSLT 理念的过程方式。你想得到什么结果?

知道预期结果后,样式表应如下所示:

<xsl:template match="Tracks">
 <tracklist>
  <sideA>
   <xsl:apply-templates select="Song[@Side='A']"/>
  </sideA>
  <sideB>
   <xsl:apply-templates select="Song[@Side='B']"/>
  </sideB>
 </tracklist>
</xsl:template>
<xsl:template match="Song">
 <track no="{@Track}" runtime="{Length}">
  <xsl:value-of select="text()"/>
 </track>
</xsl:template>
于 2012-05-14T06:51:38.567 回答
0

它看起来像一个标准的分组问题。

如果您使用的是 XSLT 2.0,那么

<xsl:for-each-group select="Song" group-by="Side">
  <xsl:element name="Side{current-grouping-key)}">
    <xsl:for-each select="current-group()">
      <track no="{position()}" runtime="{Length}">
         <xsl:value-of select="."/>
      </track>
    </xsl:for-each>
  </xsl:element>
</xsl:for-each-group>

使用 XSLT 1.0 会更困难,但仍然是一个教科书问题:查找“Muenchian Grouping”以获得答案。

于 2012-05-14T13:14:49.897 回答
0

这个 XSLT 解决方案非常简单

<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:key name="kFollowing" match="Song"
  use="generate-id(preceding-sibling::Song[not(@Side = current()/@Side)][1])"/>

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

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

 <xsl:template match="Song"/>

 <xsl:template match=
  "Song[not(@Side = preceding-sibling::Song[1]/@Side)]">

  <xsl:element name="side{@Side}">
   <xsl:apply-templates mode="inGroup" select=
   "key('kFollowing', generate-id(preceding-sibling::Song[1]/@side))"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="Song" mode="inGroup">
  <track no="{@Track}" runtime="{Length}"><xsl:value-of select="text()"/></track>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Tracks>
    <Song Side="A" Track="1">
            The Nerve
        <Length>2.58</Length>
    </Song>
    <Song Side="A" Track="2">
            Backfire
        <Length>3.22</Length>
    </Song>
    <Song Side="A" Track="3">
            Clipping
        <Length>4.05</Length>
    </Song>
    <Song Side="A" Track="4">
            Spotlight
        <Length>3.21</Length>
    </Song>
    <Song Side="A" Track="5">
            No Response
        <Length>4.01</Length>
    </Song>
    <Song Side="A" Track="6">
            Pins and Needles
        <Length>4.05</Length>
    </Song>
    <Song Side="B" Track="1">
            Goodbye
        <Length>4.09</Length>
    </Song>
    <Song Side="B" Track="2">
            Odds
        <Length>3.01</Length>
    </Song>
    <Song Side="B" Track="3">
            Electrify
        <Length>3.49</Length>
    </Song>
    <Song Side="B" Track="4">
            Armistice
        <Length>3.54</Length>
    </Song>
    <Song Side="B" Track="5">
            Lost Year
        <Length>3.13</Length>
    </Song>
    <Song Side="B" Track="6">
            Burden
        <Length>9.06</Length>
    </Song>
</Tracks>

产生了想要的正确结果:

<tracklist>
   <sideA>
      <track no="1" runtime="2.58">
            The Nerve
        </track>
      <track no="2" runtime="3.22">
            Backfire
        </track>
      <track no="3" runtime="4.05">
            Clipping
        </track>
      <track no="4" runtime="3.21">
            Spotlight
        </track>
      <track no="5" runtime="4.01">
            No Response
        </track>
      <track no="6" runtime="4.05">
            Pins and Needles
        </track>
   </sideA>
   <sideB>
      <track no="1" runtime="2.58">
            The Nerve
        </track>
      <track no="2" runtime="3.22">
            Backfire
        </track>
      <track no="3" runtime="4.05">
            Clipping
        </track>
      <track no="4" runtime="3.21">
            Spotlight
        </track>
      <track no="5" runtime="4.01">
            No Response
        </track>
      <track no="6" runtime="4.05">
            Pins and Needles
        </track>
   </sideB>
</tracklist>
于 2012-05-14T13:42:52.897 回答