如何使用 xslt 从嵌套标签中提取子元素 [英] how to extract the child element from nested tags using xslt

查看:44
本文介绍了如何使用 xslt 从嵌套标签中提取子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xml 中有一个场景:<身体><div><i>斜体</i><div id="88"><div id="4545"><h3>嘿h3</h3><xyz>XYZ</xyz>

<div id="123"><h1>示例</h1><div id="1234"><h1>标题1</h1><p>计算机</p><div><i>斜体2</i><div><h3>标题3</h3>

<div id="12345"><h1>标题1</h1>

我需要应用 div 转换为 section 的规则和其中 h1 值为 Example 的 div,删除该 h1 标记并添加属性 class=<value of that h1>到节标记.预期输出:<身体><部分><i>斜体<i></节>

<xyz>XYZ</xyz></节><section class="example"><title>标题 1</title><p>计算机</p></节><部分><i>斜体2</i></节><部分><h3>标题3</h3></节><部分><title>标题 1</title></节>我的 xslt:<xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><xsl:template match="div[h1='Example']"><xsl:apply-templates select="node()[not(self::h1)]"/></xsl:模板><xsl:template match="div/h1"><标题><xsl:apply-templates/></xsl:模板><xsl:template match="div[h3='hey h3']"><xsl:apply-templates select="node()[not(self::h3)]"/></xsl:模板><xsl:template match="div/h3"><标题><xsl:apply-templates/></xsl:模板><xsl:template match="div[not(h1='Example')]"><部分><xsl:if test="preceding-sibling::*[1][self::h1[.='Example']]"><xsl:attribute name="class">示例</xsl:attribute></xsl:if><xsl:apply-templates select="node()[not(self::div)]"/></节><xsl:apply-templates select="node()[self::div]"/></xsl:模板><xsl:template match="div[not(h3='hey h3')]"><部分><xsl:if test="preceding-sibling::*[1][self::h3[.='hey h3']]"><xsl:attribute name="class">richi rich</xsl:attribute></xsl:if><xsl:apply-templates select="node()[not(self::div)]"/></节><xsl:apply-templates select="node()[self::div]"/></xsl:模板>实际输出:<身体><部分><i>斜体</i></节><部分/><部分><title>嘿h3</title><xyz>XYZ</xyz></节><部分><title>示例</title></节><部分><title>标题 1</title><p>计算机</p></节><部分><i>斜体2</i></节><部分><title>标题 3</title></节><部分><title>标题 1</title></节>

其实有两种情况:1. 嵌套部分不应该存在并且2. 条件为heading 1",然后从div中删除该标签,并将属性添加到div中,值为tag.

你能建议我在这里做什么才能得到预期的输出.

解决方案

从身份模板开始:

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

接下来,匹配包含目标h1节点

div节点

<!-- 应用除 h1 之外的子节点 --><xsl:apply-templates select="node()[not(self::h1)]"/></xsl:模板>

以及不包含目标h1节点

div节点的模板

<部分><!-- 如果前一个兄弟节点是 h1,则设置属性 --><xsl:if test="preceding-sibling::*[1][self::h1[.='Example']]"><xsl:attribute name="class">示例</xsl:attribute></xsl:if><xsl:apply-templates select="node()[not(self::div)]"/></节><xsl:apply-templates select="node()[self::div]"/></xsl:模板>

h1 节点的模板

<标题><xsl:apply-templates/></xsl:模板>

整个样式表如下:

<xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><xsl:template match="div[h1='Example']"><xsl:apply-templates select="node()[not(self::h1)]"/></xsl:模板><xsl:template match="div/h1"><标题><xsl:apply-templates/></xsl:模板><xsl:template match="div[not(h1='Example')]"><部分><xsl:if test="preceding-sibling::*[1][self::h1[.='Example']]"><xsl:attribute name="class">示例</xsl:attribute></xsl:if><xsl:apply-templates select="node()[not(self::div)]"/></节><xsl:apply-templates select="node()[self::div]"/></xsl:模板></xsl:stylesheet>

查看实际操作(https://xsltfiddle.liberty-development.net/pPzifpb/3)

I have a scenario in xml :

    <body>
        <div><i>italic</i>
            <div id ="88">
                <div id="4545">
                    <h3>hey h3</h3>
                    <xyz>XYZ</xyz>
                </div>
            </div>
        </div>
        <div  id="123">
            <h1>Example</h1>
                <div  id="1234">
                    <h1>heading 1</h1>
                    <p>computer</p>
                    <div>
                        <i>italic 2</i>
                        <div>
                            <h3>heading 3</h3>
                        </div>
                    </div>    
                </div>
            <div  id="12345">
                <h1>heading 1</h1>
            </div>
        </div>
    </body>

I need to apply the rule that div converted to section and the div in which h1 value is Example ,delete that h1 tag and add attribute class=<value of that h1> to section tag .

expected output:
    <body>
        <section>
            <i>italic<i>
        </section>
        <section class="hey h3">

             <xyz>XYZ</xyz>
        </section>
        <section class="example">
            <title>heading 1</title>
            <p>computer</p>
        </section>
        <section>
            <i>italic 2</i>
        </section>
        <section>
        <h3>heading 3</h3>
        </section>
        <section >
            <title>heading 1</title>
        </section>
    </body>

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

    <xsl:template match="div[h1='Example']">
        <xsl:apply-templates select="node()[not(self::h1)]"/>
    </xsl:template>

    <xsl:template match="div/h1">
        <title>
            <xsl:apply-templates/>
        </title>
    </xsl:template>

    <xsl:template match="div[h3='hey h3']">
        <xsl:apply-templates select="node()[not(self::h3)]"/>
    </xsl:template>

    <xsl:template match="div/h3">
        <title>
            <xsl:apply-templates/>
        </title>
    </xsl:template>

    <xsl:template match="div[not(h1='Example')]">
        <section>
            <xsl:if test="preceding-sibling::*[1][self::h1[.='Example']]">
                <xsl:attribute name="class">example</xsl:attribute>
            </xsl:if>
            <xsl:apply-templates select="node()[not(self::div)]"/>
        </section>
        <xsl:apply-templates select="node()[self::div]"/>
    </xsl:template>

    <xsl:template match="div[not(h3='hey h3')]">
        <section>
            <xsl:if test="preceding-sibling::*[1][self::h3[.='hey h3']]">
                <xsl:attribute name="class">richi rich</xsl:attribute>
            </xsl:if>
            <xsl:apply-templates select="node()[not(self::div)]"/>
        </section>
        <xsl:apply-templates select="node()[self::div]"/>
    </xsl:template>



actual output:
    <body>
        <section>
            <i>italic</i>
        </section>
        <section/>
        <section>
            <title>hey h3</title>
            <xyz>XYZ</xyz>
        </section>
        <section>
            <title>Example</title>
        </section>
        <section>
            <title>heading 1</title>
            <p>computer</p>
        </section>
        <section>
            <i>italic 2</i>
        </section>
        <section>
            <title>heading 3</title>
        </section>
        <section>
            <title>heading 1</title>
        </section>
    </body>

Actually there are two scenarios : 1. nested section should not be present and 2. the condition that the which is having as "heading 1" then delete that tag from the div and add the attribute to the div with the value of tag .

Could you please suggest what should I do here to get the expected output.

解决方案

start with an identity template:

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

next, match the div node which contains the target h1 node

<xsl:template match="div[h1='Example']">
    <!-- apply child nodes except h1 -->
    <xsl:apply-templates select="node()[not(self::h1)]"/>
</xsl:template>

and a template for div nodes which do not contain the target h1 node

<xsl:template match="div[not(h1='Example')]">
    <section>
        <!-- set the attribute if the immediate preceding-sibling node is h1 -->
        <xsl:if test="preceding-sibling::*[1][self::h1[.='Example']]">
            <xsl:attribute name="class">example</xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="node()[not(self::div)]"/>
    </section>
    <xsl:apply-templates select="node()[self::div]"/>
</xsl:template>

and a template for the h1 node

<xsl:template match="div/h1">
    <title>
        <xsl:apply-templates/>
    </title>
</xsl:template>

The whole stylesheet is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

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

    <xsl:template match="div[h1='Example']">
        <xsl:apply-templates select="node()[not(self::h1)]"/>
    </xsl:template>

    <xsl:template match="div/h1">
        <title>
            <xsl:apply-templates/>
        </title>
    </xsl:template>

    <xsl:template match="div[not(h1='Example')]">
        <section>
            <xsl:if test="preceding-sibling::*[1][self::h1[.='Example']]">
                <xsl:attribute name="class">example</xsl:attribute>
            </xsl:if>
            <xsl:apply-templates select="node()[not(self::div)]"/>
        </section>
        <xsl:apply-templates select="node()[self::div]"/>
    </xsl:template>

</xsl:stylesheet>

see it in action (https://xsltfiddle.liberty-development.net/pPzifpb/3)

这篇关于如何使用 xslt 从嵌套标签中提取子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆