我想基于服务器提供的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>
如何将单元格放入嵌套网格?
答案 0 :(得分:1)
“失败”(更棘手的情况是)你通过<Cell Col="0" Row="0"></Cell>
概括@Row
[内部网格的一部分]与外部网格的xsl:key
相同;相同的行号,但网格深度不同。
所以你必须将它们分开并使它们独立。
一种方法:
<?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>