我正在尝试将<code-text>
包裹在div
元素中。
示例代码:
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<p>...</p>
<p>...</p>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
<p>...</p>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
<p>...</p>
<p>...</p>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
我已经在这个网站上看到了一些建议,但是当我重现他们的解决方案时,它并没有100%的效果。 我得到的最接近的是How can I wrap a group of adjacent elements using XSLT?,但出于某种原因,它也会继续添加段落。
所以这基本上就是我所在的地方:
<xsl:template match="code-text[@ATTRIBUTE]">
<xsl:element name="div">
<xsl:attribute name="class">
<xsl:text>codes</xsl:text>
</xsl:attribute>
<xsl:call-template name="code-text" />
</xsl:element>
<xsl:apply-templates select="following-sibling::node()[not(self::code-text[@ATTRIBUTE])][1]" />
</xsl:template>
<xsl:template match="code-text[@ATTRIBUTE][preceding-sibling::node()[1]/self::code-text[@ATTRIBUTE]]" name="code-text">
<xsl:text>Code goes here: </xsl:text><xsl:value-of select="translate(@ATTRIBUTE, $uppercase, $smallcase)" /><xsl:element name="br" />
<xsl:apply-templates select="following-sibling::node()[1]/self::code-text[@ATTRIBUTE]" />
</xsl:template>
但是这会输出:
<div class="codes">
Code goes here: JSON<br>
Code goes here: SOAP
</div>
<p>...</p>
Code goes here :SOAP
<p>...</p>
重复之后的最后一个SOAP和Paragraph。
我想:
<div class="codes">
Code goes here: JSON<br>
Code goes here: SOAP
</div>
<p>...</p>
etc...etc...etc
更新 澄清重复json / soap / json / soap:
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
我想成为:
<div class="codes">
Code goes here: JSON<br>
Code goes here: SOAP
</div>
<div class="codes">
Code goes here: JSON<br>
Code goes here: SOAP
</div>
而不是:
<div class="codes">
Code goes here: JSON<br>
Code goes here: SOAP<br>
Code goes here: JSON<br>
Code goes here: SOAP
</div>
答案 0 :(得分:1)
XSLT 1.0样式表
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:key name="head"
match="code-text[@ATTRIBUTE][preceding-sibling::*[1][self::code-text[@ATTRIBUTE]]]"
use="generate-id(preceding-sibling::code-text[not(preceding-sibling::*[1][self::code-text[@ATTRIBUTE]])][1])"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="code-text[not(preceding-sibling::*[1][self::code-text[@ATTRIBUTE]])]">
<div class="codes">
<xsl:apply-templates select=". | key('head', generate-id())" mode="output"/>
</div>
</xsl:template>
<xsl:template match="code-text[@ATTRIBUTE][preceding-sibling::*[1][self::code-text[@ATTRIBUTE]]]"/>
<xsl:template match="code-text" mode="output">
<xsl:text>Code goes here: </xsl:text><xsl:value-of select="translate(@ATTRIBUTE, $uppercase, $smallcase)" /><br/>
</xsl:template>
</xsl:stylesheet>
变换
<body>
<p>...</p>
<p>...</p>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
<p>...</p>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
<p>...</p>
<p>...</p>
<code-text ATTRIBUTE="JSON">...</code-text>
<code-text ATTRIBUTE="SOAP">...</code-text>
</body>
进入
<p>...</p>
<p>...</p>
<div class="codes">Code goes here: json<br>Code goes here: soap<br></div>
<p>...</p>
<div class="codes">Code goes here: json<br>Code goes here: soap<br>Code goes here: json<br>Code goes here: soap<br></div>
<p>...</p>
<p>...</p>
<div class="codes">Code goes here: json<br>Code goes here: soap<br></div>
</body>
答案 1 :(得分:1)
要为Traceback (most recent call last):
File "main.py", line 18, in
shampoo_instructions(2)
File "main.py", line 11, in shampoo_instructions
for a in num_cycles:
TypeError: 'int' object is not iterable
的每个对创建div
,您可以做到:
XSLT 1.0
<code-text ATTRIBUTE="JSON"/><code-text ATTRIBUTE="SOAP"/>
应用于以下格式正确的示例输入:
<强> XML 强>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root">
<body>
<xsl:apply-templates select="p | code-text[@ATTRIBUTE='JSON']"/>
</body>
</xsl:template>
<xsl:template match="code-text">
<div class="codes">
<xsl:value-of select="." />
<br/>
<xsl:value-of select="following-sibling::code-text[1]" />
</div>
</xsl:template>
</xsl:stylesheet>
结果将是:
<root>
<p>a</p>
<p>b</p>
<code-text ATTRIBUTE="JSON">c1</code-text>
<code-text ATTRIBUTE="SOAP">c2</code-text>
<p>d</p>
<code-text ATTRIBUTE="JSON">e1</code-text>
<code-text ATTRIBUTE="SOAP">e2</code-text>
<code-text ATTRIBUTE="JSON">f1</code-text>
<code-text ATTRIBUTE="SOAP">f2</code-text>
<p>g</p>
<p>h</p>
<code-text ATTRIBUTE="JSON">i1</code-text>
<code-text ATTRIBUTE="SOAP">i2</code-text>
</root>