首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用XSLT生成可选生成的类属性值

如何使用XSLT生成可选生成的类属性值
EN

Stack Overflow用户
提问于 2020-10-02 17:42:11
回答 1查看 43关注 0票数 0

我将使用XSLT,我不知道如何生成动态附加的类名。和class="has-title column-4"一样,我想要创建空白分隔的经典类值。

代码语言:javascript
复制
// 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之后。我想要得到,

代码语言:javascript
复制
<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>标签做一些变量,但是越来越多的错误。

在复习了答案和一些尝试之后。我在下面..。

代码语言:javascript
复制
<!-- 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~

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-02 17:56:02

我不确定我真的明白你想做什么,但这应该给你一个开始的例子。

在XSLT 1.0中

代码语言:javascript
复制
<?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://xsltfiddle.liberty-development.net/3MEcZxw

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64176063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档