【发布时间】: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>
如何将单元格放入嵌套网格中?
【问题讨论】:
-
您能否编辑您的问题以显示您在这种情况下所期望的输出?谢谢!