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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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,我正在使用XSL文件转换一些嵌套的XML。我想将两层嵌套层次结构展平为两个对象列表,同时通过添加键来保留关系 我已经将XSL设置为将每个父项的键分配给其子项,然后只输出子项注释 源XML: <?xml version="1.0" encoding="UTF-8"?> <root> <parent> <parent-id>1</parent-id> <child>

我正在使用XSL文件转换一些嵌套的XML。我想将两层嵌套层次结构展平为两个对象列表,同时通过添加键来保留关系

我已经将XSL设置为将每个父项的键分配给其子项,然后只输出子项注释

源XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <parent>
        <parent-id>1</parent-id>
        <child>
            <child-id>child_a</child-id>
        </child>
    </parent>
    <parent>
        <parent-id>2</parent-id>
        <child>
            <child-id>child_b</child-id>
        </child>
    </parent>
</root>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>


<xsl:template match="text()" /> 

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

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


<xsl:template match="child">
    <xsl:copy>
        <xsl:copy-of select="*"/>
        <foreignkey><xsl:value-of select="ancestor::parent/parent-id"/></foreignkey>
    </xsl:copy>
</xsl:template>

<xsl:template match="parent">
    <xsl:copy>
        <xsl:copy-of select="*"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

1.
儿童
2.
儿童
XSL:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <parent>
        <parent-id>1</parent-id>
        <child>
            <child-id>child_a</child-id>
        </child>
    </parent>
    <parent>
        <parent-id>2</parent-id>
        <child>
            <child-id>child_b</child-id>
        </child>
    </parent>
</root>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>


<xsl:template match="text()" /> 

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

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


<xsl:template match="child">
    <xsl:copy>
        <xsl:copy-of select="*"/>
        <foreignkey><xsl:value-of select="ancestor::parent/parent-id"/></foreignkey>
    </xsl:copy>
</xsl:template>

<xsl:template match="parent">
    <xsl:copy>
        <xsl:copy-of select="*"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这看起来正是我想要的,但我还想添加这样的父对象(现在没有嵌套的子对象):

<parent>
    <parent-id>1</parent-id>
</parent>

<parent>
    <parent-id>2</parent-id>
</parent>

1.
2.

我需要向XSL文件中添加什么才能让父对象在子对象之前或之后输出?我可以输出一个或另一个,但不能同时输出两个。

您可以在
根目录中使用
应用模板
两次:

<xsl:template match="root">
    <xsl:copy>
        <xsl:apply-templates select="parent"/>
        <xsl:apply-templates select="parent/child"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="child">
    <xsl:copy>
        <xsl:copy-of select="*"/>
        <foreignkey><xsl:value-of select="ancestor::parent/parent-id"/></foreignkey>
    </xsl:copy>
</xsl:template>

<xsl:template match="parent">
    <xsl:copy>
        <xsl:copy-of select="parent-id"/>
    </xsl:copy>
</xsl:template>

然后你得到了

<root>
   <parent>
      <parent-id>1</parent-id>
   </parent>
   <parent>
      <parent-id>2</parent-id>
   </parent>
   <child>
      <child-id>child_a</child-id>
      <foreignkey>1</foreignkey>
   </child>
   <child>
      <child-id>child_b</child-id>
      <foreignkey>2</foreignkey>
   </child>
</root>

1.
2.
儿童
1.
儿童
2.

Martin,谢谢!我试过到处打
,除了那里。