XSLT - 使用复制似乎忽略了“if”模板中的语句

发布于 2024-11-08 07:51:32 字数 2970 浏览 0 评论 0原文

我今天早些时候发布了另一个关于包含适用于不同模板的标签的查询,我收到了一些回复,帮助我获得了我想要的输出。现在,我还有一个小细节需要解决。我的 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>

我的 XSLT 看起来像:

<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>

这个输出很好,除了我希望我的行:

<balance>0.0000</balance>

显示为:

<balance/>

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

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

但仍然没有得到我想要的结果。谢谢。

I've posted another query earlier today regarding including tags that apply to different templates and I received some responses that helped me get the output I wanted. Now, I have one more small detail to resolve. My XML looks like:

<?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>

My XSLT looks like:

<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()<=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>

This is yielding output as follows:

<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>

This output is fine except that I want my lines with:

<balance>0.0000</balance>

to appear as:

<balance/>

Apologies for any typos above...I was mainly just typing rather than cut/paste. I was reading that "copy-of" can be the same as "value-of" and produce text output which could explain why the "if" clause isn't recognizing the zero value. I tried to do:

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

but still didn't get the results I want. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2024-11-15 07:51:33

尝试更改

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

<xsl:apply-templates select="following-sibling::period[position()<=2]" />

您的“句点”模板未被使用,因为节点被“根”模板消耗。

Try changing

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

to

<xsl:apply-templates select="following-sibling::period[position()<=2]" />

Your "period" template is not being used as the nodes are consumed by the "root" template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文