1

我有两个 XSL 变量,其中包含这样的数据 第一个items

 <values>
  <value>
    12635
  </value>
  <value>
    SD20
  </value>
  <value>
    2404
  </value>
   <value>
    3586877
  </value>
</values>

第二个syddata

    <syddata lastUpdated="07.11.2013 11:06 ">
  <data Varenummer="3586877" Varenavn="Liqudffid s" Brand="Georg Jensen" ></data>
  <data Varenummer="12635" Varenavn="tesr" Brand="Kähcddler" ></data>
  <data Varenummer="2404" Varenavn="uhjy" Brand="Pitcfrhstone" ></data>
  <data Varenummer="SD20" Varenavn="sfggh" Brand="dghgh" ></data>
</syddata>

我想syddata 根据items..means中的value字段的顺序进行排序itemsVarenummersyddatasyddata

    <syddata lastUpdated="07.11.2013 11:06 ">
  <data Varenummer="12635" Varenavn="tesr" Brand="Kähcddler" ></data>
  <data Varenummer="SD20" Varenavn="sfggh" Brand="dghgh" ></data>
  <data Varenummer="2404" Varenavn="uhjy" Brand="Pitcfrhstone" ></data>
  <data Varenummer="3586877" Varenavn="Liqudffid s" Brand="Georg Jensen" ></data>
</syddata>  

我已将我的整个 XSLT 添加到此处,以了解工作流程如何

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:XSLTHelper="urn:XSLTHelper"
   xmlns:exslt="http://exslt.org/common"
  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" 
  exclude-result-prefixes=" msxsl msxml ">
  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:param name="currentPage"/>
  <xsl:template match="/">
        <xsl:variable name="items" select="umbraco.library:Split($textWithPipes, '|')"/>
        <xsl:variable name="syddata" select="XSLTHelper:GenerateData($productids)"/>
  </xsl:template>

</xsl:stylesheet>
4

2 回答 2

2

为了提高效率,我首先定义一个(在 any 之外<xsl:template>

<xsl:key name="dataByNummer" match="data" use="@Varenummer" />

然后主模板可以很简单

<xsl:template match="/">
    <xsl:variable name="items" select="umbraco.library:Split($textWithPipes, '|')"/>
    <xsl:variable name="syddata" select="XSLTHelper:GenerateData($productids)"/>

    <xsl:element name="{name($syddata/*)}">
      <!-- copy attributes -->
      <xsl:sequence select="$syddata/*/@*" />
      <!-- copy data in the right order -->
      <xsl:sequence select="
          for $id in $items//value
          return key('dataByNummer', normalize-space($id), $syddata)" />
    </xsl:element>
</xsl:template>

如果它是 XSLT 1.0,它会更加繁琐,因为for在 XPath 1.0 中没有表达式,并且 XSLT 1.0key函数没有第三个参数。但它仍然可以使用嵌套for-each标签和额外变量以类似的方式完成

<xsl:template match="/">
    <xsl:variable name="items" select="umbraco.library:Split($textWithPipes, '|')"/>
    <xsl:variable name="syddata" select="XSLTHelper:GenerateData($productids)"/>

    <xsl:element name="{name($syddata/*)}">
      <!-- copy attributes -->
      <xsl:copy-of select="$syddata/*/@*" />
      <!-- copy data in the right order -->
      <xsl:for-each select="$items//value">
        <xsl:variable name="curValue" select="." />
        <xsl:for-each select="$syddata"><!-- switch focus for the key function -->
          <xsl:copy-of select="key('dataByNummer', normalize-space($curValue))" />
        </xsl:for-each>
      </xsl:for-each>
    </xsl:element>
</xsl:template>

内部for-each只是一次迭代,但关键是将“当前文档”切换到,$syddata以便key函数在其中查找data元素,而不是在$items.

于 2013-11-07T11:44:48.547 回答
2

您可以在 XSLT 2.0 中排序:

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:XSLTHelper="urn:XSLTHelper"
   xmlns:exslt="http://exslt.org/common"
  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" 
  exclude-result-prefixes=" msxml ">
  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:param name="currentPage"/>
  <xsl:template match="/">
        <xsl:variable name="items" select="umbraco.library:Split($textWithPipes, '|')"/>
        <xsl:variable name="syddata" select="XSLTHelper:GenerateData($productids)"/>

        <xsl:variable name="sorted">
          <xsl:apply-templates select="$syddata" mode="sort">
            <xsl:with-param name="values" select="$items/values/value/normalize-space()"/>
          </xsl:apply-templates>
        </xsl:variable>
        <xsl:copy-of select="$sorted"/>
  </xsl:template>

  <xsl:template match="syddata" mode="sort">
    <xsl:param name="values"/>
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:perform-sort select="data">
        <xsl:sort select="index-of($values, @Varenummer)"/>
      </xsl:perform-sort>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
于 2013-11-07T11:54:38.587 回答