【发布时间】:2015-02-04 13:37:52
【问题描述】:
我有以下输入:
<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>
【问题讨论】:
-
XML 格式不正确(没有根元素),并发布您尝试过的 XSLT。
-
源 XML 是否包含所有注释且仅注释的包装元素?
-
是的,有不止一个包装元素和其他值得注意的兄弟元素。
-
请向我们展示一个代表性和格式良好的输入示例。
标签: xml variables xslt foreach