我有以下输入:
<note>
<to>Tove</to>
<from>Jani</from>
</note>
<note>
<to>Tom</to>
<from>Eddy</from>
</note>
我希望跟随带有封闭标记的输出。:
<allnotes>
<note_t>
<to_t>Tove</to_t>
<from_t>Jani</from_t>
</note_t>
<note_t>
<to_t>Tom</to_t>
<from_t>Eddy</from-tt>
</note_t>
</allnotes>
当输入中的一个音符不完整时,例如。仅使用标记<from>
,应忽略节点元素<note>
。当所有节点元素都没有完成时,输出应该是完全空的,没有父节点</allnotes>
。我无法使最后一个条件有效。我的输出始终为<allnotes></allnotes>
这是我的XSLT。有没有办法通过编辑我的命名模板来解决最后一个条件:
<xsl:template name="test">
<xsl:variable name="to1" select="//note/to"/>
<xsl:variable name="from1" select="//note/from"/>
<xsl:if test="$to1!='' and $from1!=''">
<xsl:element name="allnotes">
<xsl:for-each select="//note">
<xsl:variable name="to" select="./to"/>
<xsl:variable name="from" select="./from"/>
<xsl:if test="$to!='' and $from!=''">
<xsl:element name="note_t">
<xsl:if test="$to!=''">
<xsl:element name="to_t">
<xsl:value-of select="$to"/>
</xsl:element>
</xsl:if>
<xsl:if test="$from!=''">
<xsl:element name="from_t">
<xsl:value-of select="$from"/>
</xsl:element>
</xsl:if>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:template>
答案 0 :(得分:1)
如果没有看到完整的输入示例,就很难提供答案。这是你可以看到的一种方式。给出如下输入:
<source>
<other/>
<note>
<to>Tove</to>
<from>Jani</from>
</note>
<note>
<to>Tom</to>
<from>Eddy</from>
</note>
</source>
以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:if test="source/note[to and from]">
<allnotes>
<xsl:copy-of select="source/note[to and from]"/>
</allnotes>
</xsl:if>
</output>
</xsl:template>
</xsl:stylesheet>
将返回:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<allnotes>
<note>
<to>Tove</to>
<from>Jani</from>
</note>
<note>
<to>Tom</to>
<from>Eddy</from>
</note>
</allnotes>
</output>
当输入为:
<source>
<other/>
<note>
<to>Tove</to>
</note>
<note>
<from>Eddy</from>
</note>
</source>
结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<output/>