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 - Fatal编程技术网

Xml xsl使用名称空间添加新元素

Xml xsl使用名称空间添加新元素,xml,xslt,Xml,Xslt,我有以下xml: <Row> <one>1</one> <two>2</two> <tree>3</tree> <four>4</four> <five>5</five> </Row> 1. 2. 3. 4. 5. 我希望得到结果: <n0:Result xmlns:ns0="http://www.my.schemas/schemas/e

我有以下xml:

<Row>

<one>1</one>
<two>2</two>
<tree>3</tree>
<four>4</four>
<five>5</five>

</Row>

1.
2.
3.
4.
5.
我希望得到结果:

<n0:Result xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
           xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
           xmlns:ns2="http://www.my.schemas/schemas/event3.xsd">
    <n1:group1>
        <n2:one>1</n2:one>
        <n2:two>2</n2:two>
    </n1:group1>

    <n1:group2>
        <n2:tree>3</n2:tree>
        <n2:four>4</n2:four>
    </n1:group2>

    <n0:five>5</n0:five>

</Result>

1.
2.
3.
4.
5.
目前我的xsl是:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
           xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
           xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
           xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" >
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>


    <xsl:template match="Row">
    <result>
                <group1>
                    <xsl:apply-templates select="one|two"/>
                </group1>
                <group2>
                    <xsl:apply-templates select="tree|four"/>
                </group2>

                <xsl:apply-templates select="five"/>
    </result>   
    </xsl:template>

</xsl:stylesheet>

如何将所需的名称空间添加到新旧元素中?
我不知道如何使用分组和新元素,只使用现有元素。

只需按照您在所需示例中所写的方式编写这些元素,就可以了

<xsl:template match="Row">
<result>
            <group1>
                <xsl:apply-templates select="one|two"/>
            </group1>
            <group2>
                <xsl:apply-templates select="tree|four"/>
            </group2>

            <xsl:apply-templates select="five"/>
</result>   
</xsl:template>

变成

<xsl:template match="Row">
<ns0:result>
            <ns1:group1>
                <xsl:apply-templates select="one|two"/>
            </ns1:group1>
            <ns1:group2>
                <xsl:apply-templates select="tree|four"/>
            </ns1:group2>

            <xsl:apply-templates select="five"/>
</ns0:result>   
</xsl:template>

显然,您需要确保其他模板也在您希望它们所属的名称空间中输出它们的元素

一个更完整的例子是

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
           xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
           xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
           xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" >
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>

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


<xsl:template match="Row">
<ns0:result>
            <ns1:group1>
                <xsl:apply-templates select="one|two"/>
            </ns1:group1>
            <ns1:group2>
                <xsl:apply-templates select="tree|four"/>
            </ns1:group2>

            <xsl:apply-templates select="five"/>
</ns0:result>   
</xsl:template>

</xsl:stylesheet>

然后,正如我所说的,如果您转换其他元素,您将需要添加模板,因此您还需要

<xsl:template match="one | two">
  <xsl:element name="ns1:{local-name()}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>



总之就是这样

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
           xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
           xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
           xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" >
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>

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


<xsl:template match="Row">
<ns0:result>
            <ns1:group1>
                <xsl:apply-templates select="one|two"/>
            </ns1:group1>
            <ns1:group2>
                <xsl:apply-templates select="tree|four"/>
            </ns1:group2>

            <xsl:apply-templates select="five"/>
</ns0:result>   
</xsl:template>

    <xsl:template match="one | two">
      <xsl:element name="ns1:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>



    <xsl:template match="three | four">
      <xsl:element name="ns2:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>



    <xsl:template match="five">
      <xsl:element name="ns0:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>


</xsl:stylesheet>


n0@Ishaked,我编辑了答案,并试图展示如何转换其他元素,使它们最终位于名称空间中。
<xsl:template match="five">
  <xsl:element name="ns0:{local-name()}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
           xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
           xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
           xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" >
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>

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


<xsl:template match="Row">
<ns0:result>
            <ns1:group1>
                <xsl:apply-templates select="one|two"/>
            </ns1:group1>
            <ns1:group2>
                <xsl:apply-templates select="tree|four"/>
            </ns1:group2>

            <xsl:apply-templates select="five"/>
</ns0:result>   
</xsl:template>

    <xsl:template match="one | two">
      <xsl:element name="ns1:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>



    <xsl:template match="three | four">
      <xsl:element name="ns2:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>



    <xsl:template match="five">
      <xsl:element name="ns0:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>


</xsl:stylesheet>