如何在没有值时删除属性,同时使用XSLT转换我的XML

时间:2017-01-09 12:15:32

标签: xml xslt xslt-2.0

我需要使用XSLT转换我的XML,只有当我的 RefID 有值时才应该 id

我的输入XML有一些需要指定的id格式:

<?xml version="1.0" encoding="UTF-8"?>
<LearningStandards>
    <LearningStandardItem xml:lang="en" RefID="B27A44AF14">
        <RefURI>L/K/2/d/</RefURI>
        <StatementCodes>
            <StatementCode>BB.DDD-K1.K2.L.1</StatementCode>
        </StatementCodes>
        <Statements>
            <Statement>Demonstrate command of the conventions.</Statement>
        </Statements>
    </LearningStandardItem>
    <LearningStandardItem xml:lang="en" RefID="">
        <RefURI>L/K/2/d/</RefURI>
        <StatementCodes>
            <StatementCode></StatementCode>
        </StatementCodes>
        <Statements>
            <Statement>Determine or clarify.</Statement>
        </Statements>
    </LearningStandardItem>
    <LearningStandardItem xml:lang="en" RefID="">
        <RefURI>L/K/2/d/</RefURI>
        <StatementCodes>
            <StatementCode />
        </StatementCodes>
        <Statements>
            <Statement>Determine or clarify.</Statement>
        </Statements>
    </LearningStandardItem>
    <LearningStandardItem xml:lang="en" RefID="C2F4CDD0">
        <RefURI>L/K/2/d/</RefURI>
        <StatementCodes>
            <StatementCode>DDSS.KLSA-LLta.KWTS.11-12.10</StatementCode>
        </StatementCodes>
        <Statements>
            <Statement>Determine or clarify.</Statement>
        </Statements>
    </LearningStandardItem>
</LearningStandards>

转换我的XSLT为:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output doctype-public="urn:pubid:eleducation.org:doctypes:dita:topic"
        doctype-system="topic.dtd" indent="yes" method="xml" />

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

    <xsl:template match="LearningStandards">
        <topic>
            <title />
            <body>
                <xsl:apply-templates />
            </body>
        </topic>
    </xsl:template>

    <xsl:template match="LearningStandardItem">
        <bodydiv outputclass="LearningStandardItem">
            <xsl:apply-templates select="@*|node()" />
        </bodydiv>
    </xsl:template>
    <xsl:template match="LearningStandardItem/@xml:lang" />
    <xsl:template match="@RefID">
        <xsl:attribute name="id">
                <xsl:value-of select="." />
            </xsl:attribute>
    </xsl:template>

    <xsl:template match="RefURI">
        <p outputclass="RefURI">
            <xsl:apply-templates />
        </p>
    </xsl:template>

    <xsl:template match="StatementCodes">
        <bodydiv outputclass="StatementCodes">
            <xsl:apply-templates />
        </bodydiv>
    </xsl:template>

    <xsl:template match="StatementCode">
        <p outputclass="StatementCode">
            <xsl:apply-templates />
        </p>
    </xsl:template>

    <xsl:template match="Statements">
        <bodydiv outputclass="Statements">
            <xsl:apply-templates />
        </bodydiv>
    </xsl:template>

    <xsl:template match="Statement">
        <xsl:variable name="code"
            select="../preceding-sibling::StatementCodes[1]/StatementCode" />
        <xsl:choose>
            <xsl:when test="string(@code)=null">
                <p outputclass="Statement">
                    <xsl:apply-templates />
                </p>
            </xsl:when>
            <xsl:otherwise>
                <p outputclass="Statement"
                    id="{string-join(tokenize($code, '\.')[position() >= last() - 1], '.')}">
                    <xsl:apply-templates />
                </p>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

转换后,我的输出XML看起来像这样。其中包含具有空值的所有&#34; id&#34; 属性:

<?xml version="1.0" encoding="UTF-8"?>
<topic>
    <title />
    <body>

        <bodydiv outputclass="LearningStandardItem" id="B27A44AF14">
            <p outputclass="RefURI">L/K/2/d/</p>
            <bodydiv outputclass="StatementCodes">
                <p outputclass="StatementCode">BB.DDD-K1.K2.L.1</p>
            </bodydiv>
            <bodydiv outputclass="Statements">
                <p outputclass="Statement" id="L.1">Demonstrate command of the
                    conventions.
                </p>
            </bodydiv>
        </bodydiv>

        <bodydiv outputclass="LearningStandardItem" id="">
            <p outputclass="RefURI">L/K/2/d/</p>
            <bodydiv outputclass="StatementCodes">
                <p outputclass="StatementCode"></p>
            </bodydiv>
            <bodydiv outputclass="Statements">
                <p outputclass="Statement" id="">Determine or clarify.</p>
            </bodydiv>
        </bodydiv>

        <bodydiv outputclass="LearningStandardItem" id="">
            <p outputclass="RefURI">L/K/2/d/</p>
            <bodydiv outputclass="StatementCodes">
                <p outputclass="StatementCode"></p>
            </bodydiv>
            <bodydiv outputclass="Statements">
                <p outputclass="Statement" id="11-12.10">Determine or clarify.</p>
            </bodydiv>
        </bodydiv>

        <bodydiv outputclass="LearningStandardItem" id="C2F4CDD0">
            <p outputclass="RefURI">L/K/2/d/</p>
            <bodydiv outputclass="StatementCodes">
                <p outputclass="StatementCode"></p>
            </bodydiv>
            <bodydiv outputclass="Statements">
                <p outputclass="Statement" id="11-12.10">Determine or clarify.</p>
            </bodydiv>
        </bodydiv>

    </body>
</topic>

但我需要输出XML只有具有值的&#34; id&#34; 属性:

<?xml version="1.0" encoding="UTF-8"?>
<topic>
    <title />
    <body>

        <bodydiv outputclass="LearningStandardItem" id="B27A44AF14">
            <p outputclass="RefURI">L/K/2/d/</p>
            <bodydiv outputclass="StatementCodes">
                <p outputclass="StatementCode">BB.DDD-K1.K2.L.1</p>
            </bodydiv>
            <bodydiv outputclass="Statements">
                <p outputclass="Statement" id="L.1">Demonstrate command of the
                    conventions.
                </p>
            </bodydiv>
        </bodydiv>

        <bodydiv outputclass="LearningStandardItem">
            <p outputclass="RefURI">L/K/2/d/</p>
            <bodydiv outputclass="StatementCodes">
                <p outputclass="StatementCode"></p>
            </bodydiv>
            <bodydiv outputclass="Statements">
                <p outputclass="Statement">Determine or clarify.</p>
            </bodydiv>
        </bodydiv>

        <bodydiv outputclass="LearningStandardItem">
            <p outputclass="RefURI">L/K/2/d/</p>
            <bodydiv outputclass="StatementCodes">
                <p outputclass="StatementCode"></p>
            </bodydiv>
            <bodydiv outputclass="Statements">
                <p outputclass="Statement">Determine or clarify.</p>
            </bodydiv>
        </bodydiv>

        <bodydiv outputclass="LearningStandardItem" id="C2F4CDD0">
            <p outputclass="RefURI">L/K/2/d/</p>
            <bodydiv outputclass="StatementCodes">
                <p outputclass="StatementCode">DDSS.KLSA-LLta.KWTS.11-12.10</p>
            </bodydiv>
            <bodydiv outputclass="Statements">
                <p outputclass="Statement" id="11-12.10">Demonstrate command of the
                    conventions.
                </p>
            </bodydiv>
        </bodydiv>

    </body>
</topic>

谢谢。

1 个答案:

答案 0 :(得分:1)

更改RefID模板以忽略空属性:

<xsl:template match="@RefID">
    <xsl:if test=". != ''">
        <xsl:attribute name="id"><xsl:value-of select="."/></xsl:attribute>
    </xsl:if>
</xsl:template>

您在Statement模板中的测试应为

<xsl:when test="$code=''">

(不是string(@code)=null,它正在检查属性 code,而不是您的变量,并且错误地测试了null而不是空字符串。)