使用xslt将特定的类值应用于最终的html5输出

时间:2017-08-10 19:28:07

标签: xml xslt-2.0

你好我有xml输入

<?xml version="1.0" encoding="UTF-8"?>
<?Xpress productline="smartdoc"?>
    <section xmlns="http://w3.com/content/3.0" id="_f6ded403-f94f-4123-a2f2-5ae75e482d88" type="document">
        <title>UI/UX Elements</title>
        <section id="_192a87c8-6754-472e-9ab3-4d760c071004" type="section">

            <body>
                <region id="_6cf44f92-1de1-4603-a4a7-1ac2e2020508" type="callout">
                    <p>
                        <t><b>Key Point</b>:&#160;There are four basic types of curation that L&amp;D departments should leverage:&#160;traditional, self, machine, and social.</t>
                    </p>
                </region>
                <p>
                    <t>&#160;</t>
                </p>
                <region id="_b0ec8f84-65ea-4590-a1da-ee6f79a2af67" type="callout">
                    <p>
                        <t><b>ANALYSIS</b>: There are four basic types of curation that L&amp;D departments should leverage: traditional, self, machine, and social.</t>
                    </p>
                </region>
                <p>
                    <t>&#160;</t>
                </p>
                <p>
                    <t>&#160;</t>
                </p>
                <region id="_72ced280-a2fe-499b-af54-c32c1414f7e1" type="callout">
                    <p>
                        <t><b>PREDICTION</b>: There are four basic types of curation that L&amp;D departments should leverage: traditional, self, machine, and social. There are four basic types of curation that L&amp;D departments should leverage: traditional, self, machine, and social.</t>
                    </p>
                </region>
            </body>
        </section>
    </section>

以下XSLT

<xsl:template match="region">
    <xsl:element name="div">
        <xsl:attribute name="id" select="@id"/>
        <xsl:choose>
            <xsl:when test="@type = 'box' and (//region/region/region[@type='box'])">
                <xsl:attribute name="class" select="'key-takeaways no-accordian'"/>
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:when test="(./p/t/b[starts-with(lower-case(.), 'prediction')]">
                <xsl:attribute name="class" select="'prediction'"/>
                <xsl:apply-templates/>
            </xsl:when>
            <!-- <xsl:when test="(//region[@type='callout'])/p/t/b[starts-with(lower-case(.), 'prediction')]">
                <xsl:attribute name="class" select="'prediction'"/>
                <xsl:apply-templates/>
            </xsl:when> -->
            <xsl:when test="(//region[@type='callout'])/p/t/b[starts-with(lower-case(.), 'key')]">
                <xsl:attribute name="class" select="'key-point'"/>
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:when test="@type = 'box'">
                <xsl:attribute name="class" select="'box-region'"/>
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:when test="@type = 'callout'">
                <xsl:attribute name="class" select="'callout-region row'"/>
                <xsl:attribute name="style" select="'width : 100%'"/>
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:attribute name="class" select="'default-region'"/>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:element>
</xsl:template>

文档的结构总是一样的,我必须根据文本中的文本应用特定的类名 / region / p / t / b节点,即如果该值是Key Point,那么如果值为Analysis,则类名将是关键点,类名是分析,依此类推。

我目前使用的xslt将预测类应用于所有元素(我认为)由于某种原因它不遵守start-with条件。我必须使用xsl选择,因为我们将来会添加更多元素,我觉得这将是检查未来添加元素的最佳方法。

感谢您的时间。

EDIT1: 最终的HTML输出

<div id="_6cf44f92-1de1-4603-a4a7-1ac2e2020508" class="key-point">
    <p>
        <b>KEY&nbsp;POIINT</b>:&nbsp;There are four basic types of curation that L&amp;D departments should leverage:&nbsp;traditional, self, machine, and social.
    </p>
</div>
<p>
    &nbsp;
</p>
<div id="_b0ec8f84-65ea-4590-a1da-ee6f79a2af67" class="key-point">
    <p>
        <b>ANALYSIS</b>: There are four basic types of curation that L&amp;D departments should leverage: traditional, self, machine, and social.
    </p>
</div>
<p>
    &nbsp;
</p>
<p>
    &nbsp;
</p>
<div id="_72ced280-a2fe-499b-af54-c32c1414f7e1" class="key-point">
    <p>
        <b>PREDICTION</b>: There are four basic types of curation that L&amp;D departments should leverage: traditional, self, machine, and social. There are four basic types of curation that L&amp;D departments should leverage: traditional, self, machine, and social.
    </p>
</div>

0 个答案:

没有答案