【问题标题】:Replacing attribute value based on multiple conditions根据多个条件替换属性值
【发布时间】:2020-09-23 09:46:29
【问题描述】:

xml文件-

<mydocument>
    <ol outputclass="abc">
        <li>
            <p>
                This is a regular ol,
                and outputclass= abc without linum.
            </p>
        </li>
        <li>
            <p>This is a regular ol,
                and outputclass= abc without linum.</p>
        </li>
    </ol>
    <ol outputclass="static">
        <li>
            <linum>3.</linum>
            <p>
                Here we use linum with an ol and outputclass=”static”.              
            </p>
        </li>
        <li>
            <linum>4.</linum>
            <p>Here we use linum with an ol and outputclass=”static”.
            </p>
        </li>
        
    </ol>
    <ol outputclass="static">
        <li>
            <p>This is ol with outputclass as static without linum element
            </p>
        </li>
    </ol>
</mydocument>

Xsl 文件-

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@outputclass">
        <xsl:attribute name="outputclass">
            <xsl:choose>
                 <xsl:when test=". ='static'">              
                    <xsl:text>continue()</xsl:text>                 
                </xsl:when>
                
                <xsl:otherwise>
                    <xsl:value-of select="." />
                </xsl:otherwise>
            </xsl:choose>
            
        </xsl:attribute>
    </xsl:template>
    <!-- drop linum elements and its value -->
    <xsl:template match="ol/li/linum">
    </xsl:template>
</xsl:stylesheet>

如果在 ol/li 标记内找到 linum 元素并且还想完全删除 linum 标记,我正在尝试将 outputclass 值从 static 替换为 continue()。我能够为我的第二个 ol 实现这一点,但它也替换了第三个 ol outputclass 值,理想情况下不应该这样做,因为它内部没有 linum 元素。

我得到的输出-

<mydocument>
    <ol outputclass="abc">
        <li>
            <p>
                This is a regular ol,
                and outputclass= abc without linum.
            </p>
        </li>
        <li>
            <p>This is a regular ol,
                and outputclass= abc without linum.
            </p>
        </li>
    </ol>
    <ol outputclass="continue()">
        <li>

            <p>Here we use linum with an ol and outputclass=”static”.
            </p>
        </li>

        <li>
            <p>Here we use linum with an ol and outputclass=”static”.
            </p>
        </li>
    </ol>
    <ol outputclass="continue()">
        <li>
            <p>This is ol with outputclass as static without linum element
            </p>
        </li>
    </ol>
</mydocument>

期望的输出-

<mydocument>
    <ol outputclass="abc">
        <li>
            <p>
                This is a regular ol,
                and outputclass= abc without linum.
            </p>
        </li>
        <li>
            <p>This is a regular ol,
                and outputclass= abc without linum.
            </p>
        </li>
    </ol>
    <ol outputclass="continue()">
        <li>

            <p>Here we use linum with an ol and outputclass=”static”.
            </p>
        </li>

        <li>

            <p>Here we use linum with an ol and outputclass=”static”.
            </p>
        </li>
    </ol>
    <ol outputclass="static">
        <li>
            <p>This is ol with outputclass as static without linum element
            </p>
        </li>
    </ol>
</mydocument>

【问题讨论】:

  • 您好,欢迎您。为什么是 [java] 标签?

标签: java xml xslt


【解决方案1】:

属性使用模板

<xsl:template match="ol[.//linum]/@outputclass[. = 'static']">
    <xsl:attribute name="outputclass">continue()</xsl:attribute>
</xsl:template>

与您拥有的其他两个模板一起。

【讨论】:

    猜你喜欢
    • 2015-12-24
    • 2018-10-29
    • 2021-07-19
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    相关资源
    最近更新 更多