我的XML里面有一些html内容.以前我可以<xsl:copy-of select="customFields/customField[@name='mainContent']/html"/>
用来将内容拉到正确的区域.一个新的要求是将<tr>
每个表中的第一个转换<tbody>
为一组thead/tr/th
.
我对如何转换感到困惑,事实上甚至没有在哪里开始:
...
<customField name="mainContent" type="Html">
<html>
<h1>Page Heading</h1>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<table cellspacing="0" cellpadding="0" summary="" border="0">
<tbody>
<tr>
<td>Heading 1</td>
<td>Heading 2</td>
<td>Heading 3</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
</tbody>
</table>
</html>
</customField>
...
Run Code Online (Sandbox Code Playgroud)
成:
...
<customField name="mainContent" type="Html">
<html>
<h1>Page Heading</h1>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<table cellspacing="0" cellpadding="0" summary="" border="0">
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
</tbody>
</table>
</html>
</customField>
...
Run Code Online (Sandbox Code Playgroud)
我的 XML 中有一些 html 内容。以前我只能使用
<xsl:copy-of select="customFields/customField[@name='mainContent']/html"/>
将内容拉入正确的区域。一个新的要求是将<tr>
每个表中的 第一个转换<tbody>
为一组thead/tr/th
.
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tbody/tr[1]">
<thead>
<tr>
<xsl:apply-templates/>
</tr>
</thead>
</xsl:template>
<xsl:template match="tbody/tr[1]/td">
<th><xsl:apply-templates/></th>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的 XML 文档时:
<customField name="mainContent" type="Html">
<html>
<h1>Page Heading</h1>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<table cellspacing="0" cellpadding="0" summary="" border="0">
<tbody>
<tr>
<td>Heading 1</td>
<td>Heading 2</td>
<td>Heading 3</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
</tbody>
</table>
</html>
</customField>
Run Code Online (Sandbox Code Playgroud)
产生完全想要的正确结果:
<customField name="mainContent" type="Html">
<html>
<h1>Page Heading</h1>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<table cellspacing="0" cellpadding="0" summary="" border="0">
<tbody>
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
</tr>
</thead>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
</tbody>
</table>
</html>
</customField>
Run Code Online (Sandbox Code Playgroud)
请注意:
使用“覆盖身份规则”设计模式。这是最基本、最强大的 XSLT 设计模式。
更新:
正如 Flynn1179 所注意到的,OP 对问题的定义(上面)与他提供的所需结果不一致。在此输出中,不仅 的第一个tr
内部tbody
被转换为thead/tr
(及其td
子级th
),而且thead
被移动到 的外部tbody
。
如果这确实是OP想要的,这里也是针对这种情况的修改解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tbody/tr[1]">
<thead>
<tr>
<xsl:apply-templates/>
</tr>
</thead>
<tbody>
<xsl:apply-templates
select="following-sibling::tr"/>
</tbody>
</xsl:template>
<xsl:template match="tbody/tr[1]/td">
<th>
<xsl:apply-templates/>
</th>
</xsl:template>
<xsl:template match="tbody">
<xsl:apply-templates select="tr[1]"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于同一个 XML 文档时,结果是:
<customField name="mainContent" type="Html">
<html>
<h1>Page Heading</h1>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p>
<table cellspacing="0" cellpadding="0" summary="" border="0">
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
<tr>
<td>sample</td>
<td>sample</td>
<td>sample</td>
</tr>
</tbody>
</table>
</html>
</customField>
Run Code Online (Sandbox Code Playgroud)