Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT-对的副本的使用似乎忽略了;如果;模板中的状态_Xml_Xslt - Fatal编程技术网

Xml XSLT-对的副本的使用似乎忽略了;如果;模板中的状态

Xml XSLT-对的副本的使用似乎忽略了;如果;模板中的状态,xml,xslt,Xml,Xslt,今天早些时候,我发布了另一个关于包含适用于不同模板的标记的查询,我收到了一些帮助我获得所需输出的回复。现在,我还有一个小细节需要解决。我的XML看起来像: <?xml version="1.0" encoding="ISO-8859-1"?> <root> <account> <name>accountA</name> </account> <period>

今天早些时候,我发布了另一个关于包含适用于不同模板的标记的查询,我收到了一些帮助我获得所需输出的回复。现在,我还有一个小细节需要解决。我的XML看起来像:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <account>
        <name>accountA</name>
    </account>
    <period>
        <type>priormonth</type>
        <balance>0.0000</balance>
    </period>
    <period>
        <type>currentmonth</type>
        <balance>20.0000</balance>
    </period>
    <account>
        <name>accountB</name>
    </account>
    <period>
        <type>priormonth</type>
        <balance>30.0000</balance>
    </period>
    <period>
        <type>currentmonth</type>
        <balance>0.0000</balance>
    </period>
</root>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/root">
        <xsl:apply-templates select="account"/>
</xsl:template>

<xsl:template match="account">
    <xsl:copy>
        <xsl:copy-of select="name" />
        <perioddata>
            <xsl:copy-of select="following-sibling::period[position()&lt;=2]" />
        </perioddata>
    </xsl:copy>
</xsl:template>

<xsl:template match="period">
    <period>
    <type> <xsl:value-of select="type"/> </type>
    <balance>
    <xsl:if test="balance != 0">
        <xsl:value-of select="balance"/>
    </xsl:if>
    </balance>
    </period>
</xsl:template>

</xsl:stylesheet>

会计
前月
0
当月
20
帐户B
前月
30
当月
0
我的XSLT看起来像:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <account>
        <name>accountA</name>
    </account>
    <period>
        <type>priormonth</type>
        <balance>0.0000</balance>
    </period>
    <period>
        <type>currentmonth</type>
        <balance>20.0000</balance>
    </period>
    <account>
        <name>accountB</name>
    </account>
    <period>
        <type>priormonth</type>
        <balance>30.0000</balance>
    </period>
    <period>
        <type>currentmonth</type>
        <balance>0.0000</balance>
    </period>
</root>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/root">
        <xsl:apply-templates select="account"/>
</xsl:template>

<xsl:template match="account">
    <xsl:copy>
        <xsl:copy-of select="name" />
        <perioddata>
            <xsl:copy-of select="following-sibling::period[position()&lt;=2]" />
        </perioddata>
    </xsl:copy>
</xsl:template>

<xsl:template match="period">
    <period>
    <type> <xsl:value-of select="type"/> </type>
    <balance>
    <xsl:if test="balance != 0">
        <xsl:value-of select="balance"/>
    </xsl:if>
    </balance>
    </period>
</xsl:template>

</xsl:stylesheet>

这将产生如下输出:

<account>
  <name>accountA</name>
  <perioddata>
      <period>
          <type>priormonth</type>
          <balance>0.0000</balance>
      </period>
      <period>
          <type>currentmonth</type>
          <balance>20.0000</balance>
      </period>
   </perioddata>
</account>
<account>
  <name>accountB</name>
  <perioddata>
      <period>
          <type>priormonth</type>
          <balance>30.0000</balance>
      </period>
      <period>
          <type>currentmonth</type>
          <balance>0.0000</balance>
      </period>
   </perioddata>
</account>

会计
前月
0
当月
20
帐户B
前月
30
当月
0
此输出很好,只是我希望我的行具有:

<balance>0.0000</balance>
0.0000
显示为:

<balance/>

对于上面的任何打字错误,我深表歉意……我主要是在打字,而不是剪切/粘贴。我读到,“copy of”可以与“value of”相同,并产生文本输出,这可以解释为什么“if”子句不能识别零值。我试着做:

<xsl:if test="number(balance) != 0">

但还是没有得到我想要的结果。谢谢。

试试换衣服

<xsl:copy-of select="following-sibling::period[position()&lt;=2]" />



您的“期间”模板未被使用,因为节点被“根”模板占用。

!成功了…谢谢。我不知道为什么我之前没有尝试,因为我确实使用了各种应用模板选项。你让我的星期五快乐!我对我的答案做了一个小改动。实际上是“根”模板消耗了“周期”节点作为副作用,因为在“根”模板中没有对它们做任何处理。这就是为什么您必须应用模板而不是复制。您的副本只是逐字复制节点,而不是调用模板。