XSLT - 使用复制似乎忽略了“if”模板中的语句
我今天早些时候发布了另一个关于包含适用于不同模板的标签的查询,我收到了一些回复,帮助我获得了我想要的输出。现在,我还有一个小细节需要解决。我的 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()<=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更改
为
您的“句点”模板未被使用,因为节点被“根”模板消耗。
Try changing
to
Your "period" template is not being used as the nodes are consumed by the "root" template.