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>

在 a<num>后面紧跟 a<head>我想将两者合并在一起。我使用这个 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::siblingpreceding::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,我希望保持不变,而我也希望保持不变。我知道这是因为它们是兄弟姐妹,即使它们之间还有其他节点。我想我想要的答案在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 回答