我无法将<panel>
标记包装到第二级单个项目,如下面的预期结果所示。相反,我将所有1.x元素值放入单个节点,并使用xslt编写。请帮帮我。
<root>
<step id="1">
<content>
<text>
1.0 Sample first level step text
</text>
</content>
<content/>
<step>
<content>
<text>
1.1 Sample second level step text
</text>
</content>
</step>
<step>
<content>
<text>
1.2 Sample second level step text
</text>
</content>
</step>
<step>
<content>
<text>
1.3 Sample second level step text
</text>
</content>
</step>
</step>
<step id="2">
<content>
<text>
2.0 Sample first level step text
</text>
</content>
<content/>
<step>
<content>
<text>
2.1 Sample second level step text
</text>
</content>
</step>
<step>
<content>
<text>
2.2 Sample second level step text
</text>
</content>
</step>
<step>
<content>
<text>
2.3 Sample second level step text
</text>
</content>
</step>
</step>
</root>
<panel>
<panel>
<panel>
1.0 Sample first level step text
</panel>
<panel>
1.1 Sample second level step text
</panel>
<panel>
1.2 Sample second level step text
</panel>
<panel>
1.3 Sample second level step text
</panel>
</panel>
<panel>
<panel>
2.0 Sample first level step text
</panel>
<panel>
2.1 Sample second level step text
</panel>
<panel>
2.2 Sample second level step text
</panel>
<panel>
2.3 Sample second level step text
</panel>
</panel>
</panel>
<xsl:template match="/">
<panel>
<xsl:apply-templates/>
</panel>
</xsl:template>
<xsl:template match="root/step" >
<panel>
<panel>
<xsl:apply-templates select ="content/text/node()"></xsl:apply-templates>
</panel>
<panel>
<xsl:apply-templates select ="step/content/text/node()"></xsl:apply-templates>
</panel>
</panel>
</xsl:template>
答案 0 :(得分:4)
这应该有效:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="root|root/step|text">
<panel>
<xsl:apply-templates/>
</panel>
</xsl:template>
</xsl:stylesheet>
修改:如果您想要美化一下,请添加以下模板:
<xsl:template match="text()">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
编辑2 :我根据新的输入和输出文档更改了模式。这是在任何其他伤口可能需要它的情况下。
答案 1 :(得分:-1)
诀窍是使用//
访问者
<xsl:template match="/">
<panel>
<xsl:apply-templates select='//text'/>
</panel>
</xsl:template>
<xsl:template match="text" >
<panel>
<xsl:apply-templates select ="./node()"></xsl:apply-templates>
</panel>
</xsl:template>
放置预期的格式而不是我的node()
<强> EDITED 强> 要按照您的要求处理分组,请再添加一个
<xsl:template match="/">
<panel>
<xsl:apply-templates select='/root/step' mode="root"/>
</panel>
</xsl:template>
<xsl:template match="step" mode="root"><!-- mode allows distinguish another tag 'step'-->
<panel> <!-- now we can convert tree to planar -->
<xsl:apply-templates select='//text'/>
</panel>
</xsl:template>
<xsl:template match="text" >
<panel>
<!-- format text from node text there -->
<xsl:apply-templates select ="text()"></xsl:apply-templates>
</panel>
</xsl:template>