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 如果特定标记具有包含特定单词的属性,如何使用XSLT附加到该标记_Xml_Xslt_Xpath - Fatal编程技术网

Xml 如果特定标记具有包含特定单词的属性,如何使用XSLT附加到该标记

Xml 如果特定标记具有包含特定单词的属性,如何使用XSLT附加到该标记,xml,xslt,xpath,Xml,Xslt,Xpath,我是新的XSLT,这是我的xml: <a> <b> <f> <g>ok</g> <h>ok</h> </f> <b test="word1">

我是新的XSLT,这是我的xml:

<a>
      <b>
                <f>
                          <g>ok</g>
                          <h>ok</h>
                </f>
                <b test="word1">
                          <d>ok1</d>
                          <e test="1">ok11</e>
                </b>
                <b test="word2">
                          <d>ok2</d>
                          <e test="1">ok22</e>
                </b>
                <b test="word3">
                          <d>ok3</d>
                          <e test="1">ok33</e>
                </b>
                <b test="word4">
                          <d>ok4</d>
                          <e test="1">ok44</e>
                </b>
      </b>

好啊
好啊
ok1
ok11
ok2
ok22
ok3
ok33
ok4
ok44

我需要输出如下所示:

<a>
      <b>
                <f>
                          <g>ok</g>
                          <h>ok</h>
                </f>
                <b test="word">
                          <d>ok1</d>
                          <e test="1">ok11</e>

                          <d>ok2</d>
                          <e test="1">ok22</e>

                          <d>ok3</d>
                          <e test="1">ok33</e>

                          <d>ok4</d>
                          <e test="1">ok44</e>
                </b >
      </b>

好啊
好啊
ok1
ok11
ok2
ok22
ok3
ok33
ok4
ok44

我需要附加'b'标记,如果它有一个包含'word'的属性


提前谢谢

XSLT-1.0解决方案:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="node()|@*">                                 <!-- identity template - generally copying all nodes -->
  <xsl:copy>
    <xsl:apply-templates select="node()|@*" />
  </xsl:copy>
</xsl:template>

<xsl:template match="b[contains(@test,'word')][1]">              <!-- copy first b node and following b node's content -->
  <b test="word">
    <xsl:copy-of select="../b[contains(@test,'word')]/*" />
  </b>
</xsl:template>

<xsl:template match="b[contains(@test,'word')]" priority="0" />  <!-- remove following b nodes -->

</xsl:stylesheet>
<a>
    <b>
        <f>
            <g>ok</g>
            <h>ok</h>
        </f>
        <b test="word">
            <d>ok1</d>
            <e test="1">ok11</e>
            <d>ok2</d>
            <e test="1">ok22</e>
            <d>ok3</d>
            <e test="1">ok33</e>
            <d>ok4</d>
            <e test="1">ok44</e>
        </b>
    </b>
</a>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="node()|@*">                                 <!-- identity template - generally copying all nodes -->
  <xsl:copy>
    <xsl:apply-templates select="node()|@*" />
  </xsl:copy>
</xsl:template>

<xsl:template match="b[contains(@test,'word')][1]">              <!-- copy first b node and following b node's content -->
  <b test="word">
    <xsl:copy-of select="../b[contains(@test,'word')]/*" />
  </b>
</xsl:template>

<xsl:template match="b[contains(@test,'word')]" priority="0" />  <!-- remove following b nodes -->

</xsl:stylesheet>
<a>
    <b>
        <f>
            <g>ok</g>
            <h>ok</h>
        </f>
        <b test="word">
            <d>ok1</d>
            <e test="1">ok11</e>
            <d>ok2</d>
            <e test="1">ok22</e>
            <d>ok3</d>
            <e test="1">ok33</e>
            <d>ok4</d>
            <e test="1">ok44</e>
        </b>
    </b>
</a>

好啊
好啊
ok1
ok11
ok2
ok22
ok3
ok33
ok4
ok44

使用XSLT 2.0/3.0,您可以对每个组使用
xsl:select=“*”group nexting=“boolean(self::b[contains(@test,$str)]”

以下是XSLT 3.0样式表:

<?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"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:param name="str" as="xs:string" select="'word'"/>

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="*[b[contains(@test, $str)]]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each-group select="*" group-adjacent="boolean(self::b[contains(@test, $str)])">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <b test="{$str}">
                            <xsl:apply-templates select="current-group()/node()"/>
                        </b>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

相反。

因此,如果
test
属性值包含子字符串
word
,您希望合并相邻的
b
元素吗?您可以使用哪个版本的XSLT?对于XSLT2.0和更高版本,您可以使用
xsl:For each group select=“*”group nexting=“contains(@test,'word')”
@MartinHonnen,我不介意使用XSLT2.0!好的,我会再试一次,非常感谢你!