Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSL:同级轴有问题。位置()是答案吗?_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

Xml XSL:同级轴有问题。位置()是答案吗?

Xml XSL:同级轴有问题。位置()是答案吗?,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.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>

我使用的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>
你可以看到,它将#12(我希望不被触碰)与“头出现”合并在一起,我也希望不被触碰。我知道这是因为他们是兄弟姐妹,即使中间还有其他节点。我想我想要的答案是在
位置()。但我在这方面没有取得成功

作为参考,所需输出如下:

<?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>

第一册。标题
1.驱魔人前传
内容

12. 我们要那个号码不动

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>


它成功了。我必须学习才能从中学到东西。我以前从未使用过自轴,我总是想知道如何使用它。现在我知道了。谢谢。您可能需要检查XPath规范:“谓词根据轴过滤节点集以生成新的节点集。对于要过滤的节点集中的每个节点,谓词Expr将使用该节点作为上下文节点进行计算”。这里重要的是被测试的节点成为上下文节点,因此
self
指的是所考虑的当前同级节点。
<?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>
<?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>