【问题标题】:xslt template doesn't apply on direct child nodesxslt 模板不适用于直接子节点
【发布时间】:2017-02-09 03:04:28
【问题描述】:

我想根据服务器提供的 xml 生成 html 标记。元素可以包含其他元素。我必须使用 XSLT 1.0

例如,我有以下 xml 结构:

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

<StackPanel DataContext="" HAlign="Left" Orientation="Vertical" Padding="5" VAlign="Top">
  <Grid Cols="2" DataContext="" HAlign="Left" Padding="5" Rows="2" VAlign="Top">
     <Cells>
         <Cell Col="1" DataContext="" HAlign="Left" Padding="5" Row="0" VAlign="Top" />
         <Cell Col="0" DataContext="" HAlign="Left" Padding="5" Row="0" VAlign="Top">
             <Grid  Cols="1" Rows="1">
               <Cells>
                  <Cell Col="0" Row="0"></Cell>
               </Cells>
             </Grid>
         </Cell>
         <Cell Col="0" DataContext="" HAlign="Left" Padding="5" Row="1" VAlign="Top" />
         <Cell Col="1" DataContext="" HAlign="Left" Padding="5" Row="1" VAlign="Top" />
      </Cells>
   </Grid>
</StackPanel>

我正在尝试应用样式:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html"/>
  <xsl:strip-space elements="*"/>

  <!--Entry point-->
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>


  <!--Cell key-->
  <xsl:key name="cell-key" match="Cells/Cell" use="@Row"/>

  <!--Grid-->
  <xsl:template match="Grid">
    <div class="grid">
      <xsl:apply-templates select="Cells/Cell[generate-id(.) = generate-id(key('cell-key', @Row))]" />
    </div>
  </xsl:template>

  <!--Grid cell-->
  <xsl:template match="Cell">
    <div class="gridRow">
      <xsl:for-each select="key('cell-key', @Row)">
        <xsl:sort select="@Col"/>
        <div class="gridCell">
          <xsl:apply-templates select="*" />
        </div>
      </xsl:for-each>
    </div>
  </xsl:template>
</xsl:stylesheet>

我使用 Muenchian 方法生成网格行。但我没有应用在直接子代上,而是得到了平面视图。

<div class="grid">
  <div class="gridRow">
    <div class="gridCell">
      <div class="grid"></div>
    </div>
    <div class="gridCell"></div>
    <div class="gridCell"></div>
  </div>
  <div class="gridRow">
    <div class="gridCell"></div>
    <div class="gridCell"></div>
  </div>
</div>

如何将单元格放入嵌套网格中?

【问题讨论】:

  • 您能否编辑您的问题以显示您在这种情况下所期望的输出?谢谢!

标签: xml xslt


【解决方案1】:

“失败”(更棘手的情况是)您通过xsl:key&lt;Cell Col="0" Row="0"&gt;&lt;/Cell&gt; [内部网格的一部分] 与外部网格的相同@Row 泛化;行号相同,但网格深度不同。

所以你必须把它们分开,让它们独立。

一种方法:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    >
    <xsl:output method="html"/>
    <xsl:strip-space elements="*"/>

    <!--Entry point-->
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>


    <!--Cell key-->
    <xsl:key name="cell-key" match="Cells/Cell" use="concat(count(ancestor::Grid), '|', @Row)"/>

    <!--Grid-->
    <xsl:template match="Grid">
        <div class="grid">
            <xsl:apply-templates select="Cells/Cell[generate-id(.) = generate-id(key('cell-key', concat(count(ancestor::Grid), '|', @Row)))]" />
        </div>
    </xsl:template>

    <!--Grid cell-->
    <xsl:template match="Cell">
        <div class="gridRow">
            <xsl:for-each select="key('cell-key', concat(count(ancestor::Grid), '|', @Row))">
                <xsl:sort select="@Col"/>
                <div class="gridCell">
                    <xsl:apply-templates select="*" />
                </div>
            </xsl:for-each>
        </div>
    </xsl:template>
</xsl:stylesheet>

结果

<div class="grid">
  <div class="gridRow">
     <div class="gridCell">
        <div class="grid">
           <div class="gridRow">
              <div class="gridCell"></div>
           </div>
        </div>
     </div>
     <div class="gridCell"></div>
  </div>
  <div class="gridRow">
     <div class="gridCell"></div>
     <div class="gridCell"></div>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多