Xml Xsl、Xpath和foreach
我想用XSL创建以下结构:Xml Xsl、Xpath和foreach,xml,xslt,xpath,Xml,Xslt,Xpath,我想用XSL创建以下结构: <div class="helloclass">C <div class="hellomethod"><p>test</p></div> <div class="hellomethod"><p>test</p></div> </div> <div class="helloclass"> </div> C 试验 试验 我有以
<div class="helloclass">C
<div class="hellomethod"><p>test</p></div>
<div class="hellomethod"><p>test</p></div>
</div>
<div class="helloclass">
</div>
C
试验
试验
我有以下XML:
<?xml version="1.0"?>
<hello id="C"></hello>
<hello id="M"></hello>
<hello id="M"></hello>
<hello id="C"></hello>
</xml>
使用XSL和Xpath,我尝试了以下方法:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
<xsl:template match="/">
<xsl:for-each select="hello">
<xsl:if test="current()[contains(@id,'C')]">
<xsl:for-each select="following-sibling">
<xsl:if test="current()[contains@id,'M']">
<p>Test</p>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:styleshet>
试验
我有一个扁平的xml结构。对于这个xml,我想基于id的a结构创建它
id=“C”表示应将其转换为
id=“M”表示应将其转换为
我的第一个目标是在正确的节点中显示文本:“test”
M也应该嵌套在C中,而不是像xml中那样是同级
还可以显示以下序列:
CMMCMMC,或CCCC MM,或CM。。。基本上我需要一个“通用”的解决方案
XLST 1.0处理器仅可用。虽然这在XSLT 2.0中是一项微不足道的任务,但如果要由XSLT 1.0处理器执行,它绝不是微不足道的 考虑以下几点:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
<xsl:template match="/">
<xsl:for-each select="hello">
<xsl:if test="current()[contains(@id,'C')]">
<xsl:for-each select="following-sibling">
<xsl:if test="current()[contains@id,'M']">
<p>Test</p>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:styleshet>
XML
<root>
<hello id="C">A</hello>
<hello id="M"></hello>
<hello id="M"></hello>
<hello id="C">B</hello>
<hello id="C">C</hello>
<hello id="M"></hello>
<hello id="M"></hello>
<hello id="M"></hello>
<hello id="C">D</hello>
<hello id="C">E</hello>
</root>
A.
B
C
D
E
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:key name="method" match="hello[@id='M']" use="generate-id(preceding-sibling::hello[@id='C'][1])" />
<xsl:template match="/root">
<body>
<xsl:for-each select="hello[@id='C']">
<div class="helloclass">
<xsl:value-of select="."/>
<xsl:for-each select="key('method', generate-id())">
<div class="hellomethod">
<p>test</p>
</div>
</xsl:for-each>
</div>
</xsl:for-each>
</body>
</xsl:template>
</xsl:stylesheet>
试验
结果
<body>
<div class="helloclass">A
<div class="hellomethod">
<p>test</p>
</div>
<div class="hellomethod">
<p>test</p>
</div>
</div>
<div class="helloclass">B</div>
<div class="helloclass">C
<div class="hellomethod">
<p>test</p>
</div>
<div class="hellomethod">
<p>test</p>
</div>
<div class="hellomethod">
<p>test</p>
</div>
</div>
<div class="helloclass">D</div>
<div class="helloclass">E</div>
</body>
A.
试验
试验
B
C
试验
试验
试验
D
E
请用文字解释逻辑。-还要注意的是,输入的XML格式不正确(没有根元素),这使得编写XSLT很困难,因为上下文未知。基本上,我在处理平面“xml”结构时遇到问题。在同一个输入中是否有两个或两个以上的序列,例如CMMCMMC?是的,我忘了提到这一点,对不起。您能使用XSLT 2.0处理器吗?非常感谢您的回答。你有什么资源可以让我学到,如果我有另一个复杂的问题,我可以自己解决吗?恐怕我不是回答这个问题的合适人选。