Xml 使用XSL将多个属性连接在一起

Xml 使用XSL将多个属性连接在一起,xml,xslt,Xml,Xslt,我有一个输入XML,如下所示 <?xml version="1.0" encoding="UTF-8"?> <topic title="My Topic" subtopic="My SubTopic"> <table title="My Table" media="print" role="center"> <thead></thead> <tbody></tbody>

我有一个输入XML,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<topic title="My Topic" subtopic="My SubTopic">
    <table title="My Table" media="print" role="center">
        <thead></thead>
        <tbody></tbody>
    </table>
</topic>
<?xml version="1.0" encoding="UTF-8"?>
<topic title="My Topic" subtitle="My Subtopic">
    <table title="My Table" outputclass="print center">
        <thead></thead>
        <tbody></tbody>
    </table>
</topic>

我想用XSL将其翻译如下

<?xml version="1.0" encoding="UTF-8"?>
<topic title="My Topic" subtopic="My SubTopic">
    <table title="My Table" media="print" role="center">
        <thead></thead>
        <tbody></tbody>
    </table>
</topic>
<?xml version="1.0" encoding="UTF-8"?>
<topic title="My Topic" subtitle="My Subtopic">
    <table title="My Table" outputclass="print center">
        <thead></thead>
        <tbody></tbody>
    </table>
</topic>

如果您注意到表的任何其他属性必须连接到名为outputclass的一个属性中,但只有表的其他属性不属于主题

我有这样一个XSL-

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="xsl xsi">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"
        standalone="no" doctype-public="-//OASIS//DTD DITA Composite//EN" doctype-system="topic.dtd"/>

    <!-- Generic element -->

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

    <xsl:template match="table">
        <table>
            <xsl:apply-templates select="@role | @media" mode="table.att"/>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates />
        </table>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:if test="local-name(.)!='noNamespaceSchemaLocation'">
            <xsl:attribute name="{local-name()}">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </xsl:if>
    </xsl:template>

   <xsl:template match="@*" mode="table.att">
        <xsl:choose>
            <xsl:when test="local-name() = 'role'">
                <xsl:attribute name="outputclass">
                    <xsl:value-of select="."></xsl:value-of>
                </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <!-- <xsl:apply-templates select="."/> -->
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

正如预期的那样,它没有起到作用。请你帮我做这个好吗。 我想我需要将所有属性发送到一起,以便将其连接起来,但我不确定如何做到这一点。

试试这个

    <xsl:template match="table">
        <table>
            <xsl:apply-templates select="@title"/>
            <xsl:if test="@role and @media">
            <xsl:attribute name="outputclass">
                <xsl:value-of select="concat(@media, ' ', @role)"/>
            </xsl:attribute>    
            </xsl:if>
            <xsl:apply-templates />
        </table>
</xsl:template>

表的任何其他属性必须连接到名为outputclass的一个属性中,但只有表的其他属性不属于主题

试着这样做:

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" 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="table">
    <xsl:copy>
        <xsl:copy-of select="@title"/>
        <xsl:attribute name="outputclass">
            <xsl:for-each select="@*[not(name()='title')]">
                <xsl:value-of select="." />
                <xsl:if test="position()!=last()">
                    <xsl:text> </xsl:text>
                </xsl:if>
            </xsl:for-each>
        </xsl:attribute>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>