Xml 如果存在多个匹配项,如何获得所有结果:XSL
我正在练习一些XSL,并将此XML文档用作一个简单的示例:Xml 如果存在多个匹配项,如何获得所有结果:XSL,xml,xslt,xpath,xml-attribute,Xml,Xslt,Xpath,Xml Attribute,我正在练习一些XSL,并将此XML文档用作一个简单的示例: <?xml version="1.1" encoding="UTF-8"?> <zoo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="zoo.xsd" > <animals> <animal type="lion">
<?xml version="1.1" encoding="UTF-8"?>
<zoo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="zoo.xsd" >
<animals>
<animal type="lion">
<name>Zeus</name>
<gender>M</gender>
<eats>antelope</eats>
<eats>monkey</eats>
</animal>
<animal type="monkey">
<name>Fredo</name>
<gender>M</gender>
<eats>banana</eats>
<iseatenby>lion</iseatenby>
</animal>
<animal type="lion">
<name>Here</name>
<gender>F</gender>
<eats>antelope</eats>
<eats>monkey</eats>
</animal>
<animal type="antelope">
<name>Annie</name>
<gender>F</gender>
<eats>grass</eats>
<iseatenby>lion</iseatenby>
</animal>
<animal type="elephant">
<name>Moses</name>
<gender>M</gender>
<eats>leaves</eats>
</animal>
</animals>
</zoo>
宙斯
M
羚羊
猴子
弗雷多
M
香蕉
狮子
在这里
F
羚羊
猴子
安妮
F
草
狮子
摩西
M
叶子
我已经能够通过我的XSL文档获得一些基本信息,但我现在只限于一件事:如果有多个结果,如何获得所有结果?例如,在我的文档中,一些动物有多个“eats”元素。我想以逗号分隔的字符串显示它们;最后,我想将每个动物的元素转换为属性,并为每个动物设置一个属性。(使用我前面的示例,新的动物元素lion的“eats”属性如下所示:eats=“羚羊,猴子”
)
有人能解释一下我将如何使用XSL做这样的事情吗??非常感谢您的帮助。:) 使用以下命令:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="zoo/animals">
<xsl:copy>
<xsl:apply-templates select="animal"/>
</xsl:copy>
</xsl:template>
<xsl:template match="animal">
<xsl:copy>
<xsl:attribute name="eats">
<xsl:for-each select="eats">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
,
参考:
position函数返回一个与上下文位置相等的数字
从表达式计算上下文。
last函数返回一个等于上下文大小的数字
表达式计算上下文
xsl:if
检查当前节点是否不是上下文中的最后一个节点。如果是,则输出,
但不是一个完美的:)
现在就试试吧,希望有帮助。。我正在将每个元素转换为属性
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//animal">
<xsl:copy>
<!--copies all other elements as attributes-->
<xsl:for-each select="*[name()!='eats']">
<xsl:attribute name="{name()}">
<xsl:apply-templates select = "text()"/>
</xsl:attribute>
</xsl:for-each>
<xsl:attribute name="eats">
<!-- Go to <eats/> node -->
<xsl:for-each select="eats">
<!--insearts string ", " if it has preceding values already :) -->
<xsl:if test="preceding-sibling::eats">
<xsl:text>, </xsl:text>
</xsl:if>
<!--copies all the text :) -->
<xsl:apply-templates select="text()"/>
</xsl:for-each>
</xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
,
您能解释一下xsl:if吗?这只是说如果这是同一个元素,那么就附加它吗?另外,你能帮我回忆一下什么位置()和最后一次()返回吗?编辑:我刚刚在W3学校的XSLT-Tryit编辑器()上试用了你的答案,但似乎不起作用:/@ChrisV.,我已经更新了我的答案。也许这个测试仪只支持XHTML
输出,因为我测试了我的代码,它工作得很好。回答和解释很好,基里尔:)谢谢你!W3学校的测试人员似乎也不喜欢这个答案:(它只是输出一个空白页。我将在其他地方尝试。哦!实际上我缺少
语句,我认为W3学校不喜欢它。):(让我编辑它并发布。。我实际上注意到,就在一秒钟前,我尝试添加了以下内容,但没有效果:(我可以在Eclipse之类的IDE或记事本++之类的环境中测试代码吗?除了W3学校的在线测试仪之外,我还没有任何测试xsl的经验。@ChrisV.bro在W3学校里你不会得到任何输出:(它只支持html输出..:|@infantprogrammer'Aravind':不客气:).BTW,我不推荐xsltcake——在线转换执行器不可能在任何时候都可靠且可用。此外,他们使用的是用户浏览器的XSLT处理器,因此通常两个使用不同浏览器的不同用户从同一转换中获得不同的结果。