Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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 XSLT2从嵌套输入结构生成多个输出_Xml_Xslt_Xslt 2.0_Saxon - Fatal编程技术网

Xml XSLT2从嵌套输入结构生成多个输出

Xml XSLT2从嵌套输入结构生成多个输出,xml,xslt,xslt-2.0,saxon,Xml,Xslt,Xslt 2.0,Saxon,我使用XSLT2.0(Saxon PE 9.5)从输入创建多个XML文件,使用xsl:result-document。每个项目可以包含多个项目,在一个项目中有typeA和typeB。对于每种这样的类型,我都要生成XML和XML。 示例: input.xml: <task> <items> <item> <typeA> <id>A1</id> ... <

我使用XSLT2.0(Saxon PE 9.5)从输入创建多个XML文件,使用
xsl:result-document
。每个项目可以包含多个项目,在一个项目中有typeA和typeB。对于每种这样的类型,我都要生成XML和XML。 示例:
input.xml

<task>
  <items>
    <item>
      <typeA>
        <id>A1</id>
        ...
      </typeA>
      <typeB>
        <id>B</id>
        ...
      </typeB>
    </item>
    <item>
      <typeA>
        <id>A2</id>
        ...
      </typeA>
    </item>
  </items>
</task>

A1
...
B
...
A2
...
所需输出:
out1_typea.xml

<task>
  <items>
    <item>
      <typeA>
        <id>A</id>
        ...
      </typeA>
    </item>
  </items>
</task>
<task>
  <items>
    <item>
      <typeB>
        <id>B</id>
        ...
      </typeB>
    </item>
  </items>
</task>
<task>
  <items>
    <item>
      <typeA>
        <id>A2</id>
        ...
      </typeA>
    </item>
  </items>
</task>

A.
...
out1\u typeb.xml

<task>
  <items>
    <item>
      <typeA>
        <id>A</id>
        ...
      </typeA>
    </item>
  </items>
</task>
<task>
  <items>
    <item>
      <typeB>
        <id>B</id>
        ...
      </typeB>
    </item>
  </items>
</task>
<task>
  <items>
    <item>
      <typeA>
        <id>A2</id>
        ...
      </typeA>
    </item>
  </items>
</task>

B
...
out2_typea.xml

<task>
  <items>
    <item>
      <typeA>
        <id>A</id>
        ...
      </typeA>
    </item>
  </items>
</task>
<task>
  <items>
    <item>
      <typeB>
        <id>B</id>
        ...
      </typeB>
    </item>
  </items>
</task>
<task>
  <items>
    <item>
      <typeA>
        <id>A2</id>
        ...
      </typeA>
    </item>
  </items>
</task>

A2
...
我开始使用以下转换,该转换基于项目进行拆分:

  <xsl:template match="/task">
    <xsl:for-each select="items/item">
      <xsl:result-document href="out{position()}.xml">
        <task xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <items>
            <item>
              <xsl:copy-of select="*" />
            </item>
          </items>
          <xsl:copy-of select="../../(* except items)" />
        </task>
      </xsl:result-document>
    </xsl:for-each>
  </xsl:template>

但我不知道如何在类型上进一步划分。

这不只是:

<xsl:template match="/task">
  <xsl:for-each select="items/item">
    <xsl:variable name="pos" select="position()"/>
    <xsl:for-each select="*">
      <xsl:result-document href="out{$pos}_{name()}.xml">
        <task>
          <items>
            <item>
              <xsl:copy-of select="." />
            </item>
          </items>
          <xsl:copy-of select="../../../(* except items)" />
        </task>
      </xsl:result-document>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>


谢谢师父!只是一个小问题:位置基于类型元素的位置。我不生成out1_typeA、out2_typeB、out3_typeA,而是如何根据项目进行定位,即:out1_typeA、out1_typeB、out2_typeA?我知道您的时间很紧张,但您能否对答案进行解释?或者,如果你愿意,我可以自己编辑解释。@Mathias,我不确定哪些部分需要解释。这里可能使用了十几个重要的XSLT/XPath特性,它们对我来说都是非常基本的。其中哪一个对您来说似乎不明显?我认为您的代码很容易理解,但在我看来,回答“不知道如何在类型上进一步划分”的问题将改进答案。类似于:
xsl:for each
语句的内容可以嵌套为“进一步拆分”,但要确保跟踪外部上下文项在
xsl:variable
中的位置。