XSLT - 标签不匹配
我正在尝试将 XLST 与此 XML 结合使用:
<?xml version="1.0"?>
<ArrayOfDynamicData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DynamicData>
<item>
<name>Name</name>
<value xsi:type="xsd:int">0</value>
</item>
<item>
<name>Value</name>
<value xsi:type="xsd:long">1</value>
</item>
</DynamicData>
</ArrayOfDynamicData>
我目前拥有的 XLST 是:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs xdt err fn">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<graph showNames='1' caption='##TITLE HERE##' decimalPrecision='0' >
<xsl:for-each select="ArrayOfDynamicData/DynamicData/item">
<xsl:if test="name='Name'">
<set>
<name><xsl:value-of select='value' /></name>
</xsl:if>
<xsl:if test="name='Value'">
<value><xsl:value-of select='value' /></value>
</set>
</xsl:if>
</xsl:for-each>
</graph>
</xsl:template>
</xsl:stylesheet>
现在在我的脑海中,这应该可以工作,但由于标签不匹配,我收到错误。但我需要将两个块中的数据放置在一个块中,并且我不确定如何实现这一点。
谢谢, 詹姆斯。
编辑:
我试图实现这一目标:
<set>
<name>0</name>
<value>2</value>
</set>
如果我将
标记放在 if 之外,我会得到:
<set>
<name>0</name>
</set>
<set>
<value>2</value>
</set>
I'm trying to use XLST with this XML:
<?xml version="1.0"?>
<ArrayOfDynamicData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DynamicData>
<item>
<name>Name</name>
<value xsi:type="xsd:int">0</value>
</item>
<item>
<name>Value</name>
<value xsi:type="xsd:long">1</value>
</item>
</DynamicData>
</ArrayOfDynamicData>
The XLST i have at the minute is:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs xdt err fn">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<graph showNames='1' caption='##TITLE HERE##' decimalPrecision='0' >
<xsl:for-each select="ArrayOfDynamicData/DynamicData/item">
<xsl:if test="name='Name'">
<set>
<name><xsl:value-of select='value' /></name>
</xsl:if>
<xsl:if test="name='Value'">
<value><xsl:value-of select='value' /></value>
</set>
</xsl:if>
</xsl:for-each>
</graph>
</xsl:template>
</xsl:stylesheet>
Now in my head this should work, but i get an error due to the mismatched tags. But i need data from two blocks to be placed within one block and i'm not sure how i can achieve this.
Thanks,
james.
Edit:
I'm trying to achieve this:
<set>
<name>0</name>
<value>2</value>
</set>
If i put the <set>
tags outside the if's, I get:
<set>
<name>0</name>
</set>
<set>
<value>2</value>
</set>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可以通过非常简单的方式完成:
应用于提供的 XML 文档时:
生成所需的正确结果:
This can be done in a very simple way:
when applied on the provided XML document:
the wanted, correct result is produced:
实际上,您是在以这种方式独立处理两个
item
元素,这意味着它们永远不会在输出中组合成单个set
元素。如果您可以保证值始终跟随名称,那么您可以这样做:You're actually treating both
item
elements independently doing it this way, which means they won't ever get combined into a singleset
element in the output. If you can guarantee that a value always follows a name, then you can do this:这里缺少
:
Edit
仔细检查后,
标记脱离了结构。仔细看看
的整个正文:There is an
</xsl:if>
missing here:Edit
At closer inspection, the
<set>
tag falls out of the structure. Take a good look at the entire body of<xsl:for-each>
: