【问题标题】:XSLT XML: The output of the trasformation should be empty, when some children nodes are missingXSLT XML:当缺少一些子节点时,转换的输出应该是空的
【发布时间】: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>

当输入中的一个音符不完整时,例如。只有带有标签&lt;from&gt; 的节点元素&lt;note&gt; 应该被忽略。当所有节点元素都不完整时,输出应该完全为空,没有父节点&lt;/allnotes&gt;。我无法使最后一个条件起作用。我的输出总是&lt;allnotes&gt;&lt;/allnotes&gt;

这是我的 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


【解决方案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/>

【讨论】:

  • 好的,我在我的 XSLT 中试试这个:
  • 我正在测试和测试,每次输出都是正确的。我刚刚用 替换了我的 XSLT 中的第一行树。谢谢你的帮助!!!!
  • @sri 如果您的问题得到解答,请通过接受答案关闭它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 2012-03-14
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多