Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
XSLT2.0中XML到XML内部节点结构的转换_Xml_Xslt - Fatal编程技术网

XSLT2.0中XML到XML内部节点结构的转换

XSLT2.0中XML到XML内部节点结构的转换,xml,xslt,Xml,Xslt,我试图解决这个问题已经有一段时间了 我有一个xml文件: <ParentNode> <RequirementGroup> <ID>id1</ID> <Requirement> <ID>id2</ID> <Description>value1</Description> </Requirement> <Req

我试图解决这个问题已经有一段时间了

我有一个xml文件:

<ParentNode>
 <RequirementGroup>
    <ID>id1</ID>
    <Requirement>
        <ID>id2</ID>
        <Description>value1</Description>
    </Requirement>
    <RequirementGroup pi="attribute">
        <ID>id3</ID>
        <Requirement>
            <ID>id4</ID>
            <Description>value2</Description>
        </Requirement>
        <Requirement>
            <ID>id5</ID>
            <Description>value3</Description>
        </Requirement>
        <Requirement>
            <ID>id6</ID>
            <Description>value4</Description>
        </Requirement>
        <Requirement>
            <ID>id7</ID>
            <Description>value5</Description>
        </Requirement>
        <RequirementGroup>
            <ID>id8</ID>
            <Requirement>
                <ID>id9</ID>
                <Description>value6</Description>
            </Requirement>
            <RequirementGroup pi="attribute">
                <ID>id10</ID>
                <Requirement>
                    <ID>id11</ID>
                    <Description>value7</Description>
                </Requirement>
            </RequirementGroup>
        </RequirementGroup>
    </RequirementGroup>
 </RequirementGroup>
</ParentNode>
XML1的示例节点:

 <cbc:ID schemeID="CriteriaID" schemeAgencyID="EU-COM-GROW" schemeVersionID="1.0">

XML2的示例:

<cbc:ID schemeID="CriteriaTaxonomy" schemeAgencyID="EU-COM-GROW" schemeVersionID="02.00.00">

但是,当我将名称空间从XML2复制到我的(您建议的XSLT)文件时(因为这样我认为它会自动重新创建新名称空间,就像在XML2中一样,不会有问题,转换也可以)。但不知怎么的它看起来很奇怪

输出为:

<cbc:ID xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccv-cbc="urn:isa:names:specification:ubl:schema:xsd:CCV-CommonBasicComponents-1" xmlns:cev="urn:isa:names:specification:ubl:schema:xsd:CEV-CommonAggregateComponents-1" xmlns:cev-cbc="urn:isa:names:specification:ubl:schema:xsd:CEV-CommonBasicComponents-1" xmlns:espd="urn:grow:names:specification:ubl:schema:xsd:ESPDResponse-1" xmlns:espd-cac="urn:grow:names:specification:ubl:schema:xsd:ESPD-CommonAggregateComponents-1" xmlns:espd-cbc="urn:grow:names:specification:ubl:schema:xsd:ESPD-CommonBasicComponents-1" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" schemeID="CriteriaID" schemeAgencyID="EU-COM-GROW" schemeVersionID="1.0">


在输出中,它添加节点旁边的所有名称空间。。为什么?如何将所需的名称空间版本等从一个转换为另一个?就像在XML2中更改节点的属性一样。

我不能完全理解您想要什么,但从我目前掌握的情况来看,您需要一个标识模板,它是

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
xsl:apply-templates
本身是递归的。整个样式表如下所示:

<?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"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="RequirementGroup">
        <Node1>
            <xsl:apply-templates/>
        </Node1>
    </xsl:template>

    <xsl:template match="Requirement">
        <Example>
            <xsl:apply-templates/>
        </Example>
    </xsl:template>

   <xsl:template match="ParentNode">
       <ParentNode>
           <xsl:apply-templates/>
       </ParentNode>
   </xsl:template>

</xsl:stylesheet>


只需向其中添加其他模板即可更改值。

请编辑您的问题,并在XML中指定所需的输出。您好,Joel感谢您的建议,我添加了所需的输出,该输出与第一个XML文件实际上相同,但具有不同的节点名称,并在itJoel中添加了一些子节点,谢谢您的解决方案。它正在工作,但我有一个问题:我想要转换的XML1和XML2,它们有一些不同。例如,有些节点在XML1中不存在,但必须在XML2中,或者名称空间和版本不同。如果我只应用模板,它不会应用结构中缺少的节点或其他内容。这就是为什么我尝试创建自己的模板,并根据结构调用它们。我如何解决这个问题?应用模板很简单是的,但它们并不完全相同。希望我能告诉自己,如果你能提供一个我可以研究的最小的例子,那就太好了。包括名称空间和当前XML中的所有内容。还包括所需的输出。乔尔,我试图解释上述问题,希望你也明白。我真的很感谢你的帮助。。若你们有更多的问题,我会帮助你们理解这个问题。非常感谢。我明白了,只需在最后一个名称空间声明之后添加exclude result prefixes=“#all”。Joel,我在它不起作用之前尝试过它。一些名称空间仍然存在
 <cbc:ID schemeID="CriteriaID" schemeAgencyID="EU-COM-GROW" schemeVersionID="1.0">
<cbc:ID schemeID="CriteriaTaxonomy" schemeAgencyID="EU-COM-GROW" schemeVersionID="02.00.00">
<cbc:ID xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccv-cbc="urn:isa:names:specification:ubl:schema:xsd:CCV-CommonBasicComponents-1" xmlns:cev="urn:isa:names:specification:ubl:schema:xsd:CEV-CommonAggregateComponents-1" xmlns:cev-cbc="urn:isa:names:specification:ubl:schema:xsd:CEV-CommonBasicComponents-1" xmlns:espd="urn:grow:names:specification:ubl:schema:xsd:ESPDResponse-1" xmlns:espd-cac="urn:grow:names:specification:ubl:schema:xsd:ESPD-CommonAggregateComponents-1" xmlns:espd-cbc="urn:grow:names:specification:ubl:schema:xsd:ESPD-CommonBasicComponents-1" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" schemeID="CriteriaID" schemeAgencyID="EU-COM-GROW" schemeVersionID="1.0">
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="RequirementGroup">
    <Node1>
        <xsl:apply-templates/>
    </Node1>
</xsl:template>

<xsl:template match="Requirement">
    <Example>
        <xsl:apply-templates/>
    </Example>
</xsl:template>
<?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"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="RequirementGroup">
        <Node1>
            <xsl:apply-templates/>
        </Node1>
    </xsl:template>

    <xsl:template match="Requirement">
        <Example>
            <xsl:apply-templates/>
        </Example>
    </xsl:template>

   <xsl:template match="ParentNode">
       <ParentNode>
           <xsl:apply-templates/>
       </ParentNode>
   </xsl:template>

</xsl:stylesheet>