我想基于服务器提供的xml生成html标记。元素可以包含其他元素。我必须使用XSLT 1.0
例如,我具有followinng 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>
如何将单元放置到嵌套网格中?
您将“故障”(更棘手的情况是)通过推广为<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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句