Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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

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
使用XSLT更改XML根节点_Xml_Xslt - Fatal编程技术网

使用XSLT更改XML根节点

使用XSLT更改XML根节点,xml,xslt,Xml,Xslt,我是XSLT新手。我想根据其他子节点的条件更改XML中的根节点。但是保持子节点始终相同。例如,我有以下XML: <Root1> ................ ................ <Root2> ................ ................ <AnimalOrPlant> <Type>A</Type>

我是XSLT新手。我想根据其他子节点的条件更改XML中的根节点。但是保持子节点始终相同。例如,我有以下XML:

   <Root1>
    ................
    ................

    <Root2>
    ................
    ................

        <AnimalOrPlant>
             <Type>A</Type>
              <Food1>Something</Food1>
              <Food2>Somthing11</Food2>
              <Name>ant</Name>
              <Color>Black</GIIN>
              <Waterconsumption>5lt</Waterconsumption>
        </AnimalOrPlant>  
    ................
    ................

    </Root2>
    ................
    ................
</Root1>

................
................
................
................
A.
某物
忧郁的11
蚂蚁
黑色
5lt
................
................
................
................
我想将XML更改为:

<Root1>
    ................
    ................
    <Root2>
    ................
    ................
        <Animal>
             <Type>A</Type>
              <Food1>Something</Food1>
              <Food2>Somthing11</Food2>
              <Name>ant</Name>
              <Color>Black</Color>
              <Waterconsumption>5lt</Waterconsumption>
        </Animal>
     ................
    ................ 
    </Root2>
    ................
    ................
</Root1>

................
................
................
................
A.
某物
忧郁的11
蚂蚁
黑色
5lt
................
................ 
................
................
这意味着依赖于
=A
,我将节点
更改为
。如果
=P
,我会将其更改为
。我已经编写了以下XSLT:

<xsl:template match="Root1">
 <xsl:choose>
    <xsl:when test="Root2/AnimalOrPlant/Type='A'">
        <Animal>
            <xsl:element name ="Type">
              <xsl:value-of select="Root2/AnimalOrPlant/Type"/>
            </xsl:element>
            <xsl:element name ="Food1">
              <xsl:value-of select="Root2/AnimalOrPlant/Food1"/>          
            </xsl:element>
            <xsl:element name ="Food2">
              <xsl:value-of select="Root2/AnimalOrPlant/Food2"/>
            </xsl:element>
            <xsl:element name ="Name">
              <xsl:value-of select="<xsl:value-of select="Root2/AnimalOrPlant/Name"/>
            </xsl:element>
            <xsl:element name ="Color">
              <xsl:value-of select="<xsl:value-of select="Root2/AnimalOrPlant/Color"/>
            </xsl:element>
            <xsl:element name ="Waterconsumption">
              <xsl:value-of select="<xsl:value-of select="Root2/AnimalOrPlant/Waterconsumption"/>
            </xsl:element>
        </Animal>
    </xsl:when>
    <xsl:when test="Root2/AnimalOrPlant/Type='P'">
        <Plant>
            <xsl:element name ="Type">
              <xsl:value-of select="Root2/AnimalOrPlant/Type"/>
            </xsl:element>
            <xsl:element name ="Food1">
              <xsl:value-of select="Root2/AnimalOrPlant/Food1"/>          
            </xsl:element>
            <xsl:element name ="Food2">
              <xsl:value-of select="Root2/AnimalOrPlant/Food2"/>
            </xsl:element>
            <xsl:element name ="Name">
              <xsl:value-of select="<xsl:value-of select="Root2/AnimalOrPlant/Name"/>
            </xsl:element>
            <xsl:element name ="Color">
              <xsl:value-of select="<xsl:value-of select="Root2/AnimalOrPlant/Color"/>
            </xsl:element>
            <xsl:element name ="Waterconsumption">
              <xsl:value-of select="<xsl:value-of select="Root2/AnimalOrPlant/Waterconsumption"/>
            </xsl:element>
        </Plant>
    </xsl:when>
</xsl:choose>   
</xsl:template>


这是直截了当的,如果你了解

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="AnimalOrPlant[Type='A']">
        <Animal>
            <xsl:apply-templates select="@*|node()"/>
        </Animal>
    </xsl:template>

    <xsl:template match="AnimalOrPlant[Type='P']">
        <Plant>
            <xsl:apply-templates select="@*|node()"/>
        </Plant>
    </xsl:template>

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

我建议您这样做:

XSLT1.0

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

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

<xsl:template match="AnimalOrPlant">
    <xsl:variable name="name">
        <xsl:choose>
            <xsl:when test="Type='A'">Animal</xsl:when>
            <xsl:when test="Type='P'">Plant</xsl:when>
            <xsl:otherwise>Unknown</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$name}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

动物
种植
不为人知


注意:这与根节点甚至根元素无关。

谢谢。我在和中还有其他节点。我想要的是动态地创建和更新。标识模板负责所有其他节点。我收到了此错误。“xsl:template”不能是“Root2”元素的子元素。因为我在root2中使用“xsl:template”来处理其他项目。我可以在同一个XSLT中使用多个xsl:template吗?您可以在中看到上面的XSLT。听起来您只是从答案中复制了一个模板,并将其添加到XSLT中的错误位置。您应该真正使用整个样式表来代替当前使用的样式表。谢谢谢谢michael,我收到了这个错误。“xsl:template”不能是“Root2”元素的子元素。因为我在root2中将“xsl:template”用于其他项目。我可以在同一XSLT中使用多个xsl:template吗?@Shuvra模板不能嵌套。如果您有一个与
Root2
匹配的模板,请将其与其他模板一起放置,并确保它包含合适的
xsl:apply templates
指令,以将上下文传递给与
AnimalOrPlant
匹配的模板。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="AnimalOrPlant[Type='A']">
        <Animal>
            <xsl:apply-templates select="@*|node()"/>
        </Animal>
    </xsl:template>

    <xsl:template match="AnimalOrPlant[Type='P']">
        <Plant>
            <xsl:apply-templates select="@*|node()"/>
        </Plant>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="AnimalOrPlant">
    <xsl:variable name="name">
        <xsl:choose>
            <xsl:when test="Type='A'">Animal</xsl:when>
            <xsl:when test="Type='P'">Plant</xsl:when>
            <xsl:otherwise>Unknown</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$name}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>