0

私は近似するxmlを持っています:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <num>Book 1.</num>
    <head> Title</head>
    <chapter>
        <num>1.</num>
        <head> The Begining</head>
        <p>content</p>
    </chapter>
    <num>12. </num><p>we want that number untouched</p>
    <chapter>
        <num>2.</num>
        <head> The Middle</head>
        <p>content</p>
    </chapter>
    <head>Heads Occur</head><p>we want that head untouched</p>
</book>

<num>a の直後に a が続く場合<head>、2 つをマージしたいと思います。私はこの xsl を使用して、ある程度の成功を収めましたが、すべてのユースケースではありません。

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

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

    <xsl:template match="num[following-sibling::head]">
        <mergedhead>
            <xsl:apply-templates select="node()|@*"/>
            <xsl:value-of select="following-sibling::head"/>
        </mergedhead>    
    </xsl:template>
    <!-- keep the old head from showing up in the new output-->
    <xsl:template match="head[preceding-sibling::num]"/>

</xsl:stylesheet>

following::sibling機能しpreceding::siblingますが、すべてのユースケースではありません。時々それらは引き込まれ<num><head>それは互いに直接隣り合っていません. 私の欠陥のある XSL の出力:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <mergedhead>Book 1. Title</mergedhead>
    <chapter>
        <mergedhead>1. The Begining</mergedhead>
        <p>content</p>
    </chapter>
    <mergedhead>12. Heads Occur</mergedhead><p>we want that number untouched</p>
    <chapter>
        <mergedhead>2. The Middle</mergedhead>
        <p>content</p>
    </chapter>
    <p>we want that head untouched</p>
</book>

そのままにしておきたい #12 と、同じくそのままにしておきたい 'heads occur' をマージしたことがわかります。間に他のノードがあっても、それらが兄弟であるためだと私は知っています。私が望む答えは にあると思いますposition()。しかし、私はそれで成功していません。

参考までに、目的のアウトは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <mergedhead>Book 1. Title</mergedhead>
    <chapter>
        <mergedhead>1. The Begining</mergedhead>
        <p>content</p>
    </chapter>
    <num>12. </num><p>we want that number untouched</p>
    <chapter>
        <mergedhead>2. The Middle</mergedhead>
        <p>content</p>
    </chapter>
    <head>Heads Occur</head><p>we want that head untouched</p>
</book>
4

1 に答える 1

2

要件を満たしている場合は、次の xslt を試してください。

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

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

    <!-- Take all following sibling elements (i.e. following-sibling:*) regardless their names, from them take first one (i.e. [1]) and test it if it is head (i.e. [self::head]-->
    <xsl:template match="num[following-sibling::*[1][self::head]]">
        <mergedhead>
            <xsl:apply-templates select="node()|@*"/>
            <!-- Take the first following sibling -->
            <xsl:value-of select="following-sibling::head[1]"/>
        </mergedhead>    
    </xsl:template>
    <!-- similar to num template -->
    <xsl:template match="head[preceding-sibling::*[1][self::num]]"/>

</xsl:stylesheet>
于 2013-09-21T13:08:21.353 に答える