我将使用XSLT,我不知道如何生成动态附加的类名。和class="has-title column-4"一样,我想要创建空白分隔的经典类值。
// Original XML string..
<contents>
<box type="list" mark="kr-con">
<li>test texts..</li>
<li>test texts..</li>
..
<li>test texts..</li>
</box>
</contents>在XSLTProcessing之后。我想要得到,
<div class="box box-list column-1">
<li>test texts..</li>
<li>test texts..</li>
.. (processed elements)
<li>test texts..</li>
</div>在原始xml中,box[@type]具有默认值list,因此原始xml字符串不具有此属性,尽管结果具有box-list类。此外,box[@column]属性不存在,但具有默认值1,并导致column-1类。
。。
差不多吧..。
我已经试了这么多小时了,我想我不能处理这件事。XSL的事。非常沮丧..。但仍然需要..。
如何生成类值?
我试着用<xsl:variable>标签做一些变量,但是越来越多的错误。
在复习了答案和一些尝试之后。我在下面..。
<!-- figure 처리 -->
<xsl:template match="figure">
<!-- align 속성 처리 -->
<xsl:variable name="align">
<xsl:choose>
<xsl:when test="not(@align) and parent::p and position()=1">
<xsl:value-of select="'right'"/>
</xsl:when>
<xsl:when test="not(@align)">
<xsl:value-of select="'center'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@align"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- width 처리 -->
<xsl:variable name="width">
<xsl:choose>
<xsl:when test="not(@width)">
<xsl:value-of select="'width-6'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@width"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- 주 요소 처리 -->
<xsl:choose>
<xsl:when test="parent::li">
<img src="{@url}" class="width-12"/>
</xsl:when>
<xsl:otherwise>
<img src="{@url}" class="align-{@align} {@width}"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>lol~
发布于 2020-10-02 17:56:02
我不确定我真的明白你想做什么,但这应该给你一个开始的例子。
在XSLT 1.0中
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="contents/box">
<xsl:variable name="box">
<xsl:choose>
<xsl:when test="@type='list'">
<xsl:value-of select="'box-list'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@type"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="col">
<xsl:choose>
<xsl:when test="@column">
<xsl:value-of select="@column"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'1'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<div class="{concat('box',' ',$box,' ','column-',$col)}">
<xsl:copy-of select="li"/>
</div>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/64176063
复制相似问题