使用基于相同子节点的XSLT拆分XML并使用其他信息保留Header部分

时间:2014-03-24 20:06:20

标签: xml xslt

要求: 使用相同的节点拆分XML主体,并在标头标记中保留原始标记,并从xml正文部分的子节点追加唯一的Id。

e.g:

<Document>
<Header>
    <Time>2011-04-22T10:57:00.000-04:00</Time>
</Header>
<Child>
    <submit>
        <application>
            ...
        </application>
    </submit>
    <IssuingAgency>
        ...
        <AgentIdentification>
            <IdentificationID>103701978</IdentificationID>
        </AgentIdentification>
        <AgentSequenceID>01</AgentSequenceID>
        <AgentSatus>
            <StatusText>ABC</StatusText>
            <StatusDescriptionText>Initial</StatusDescriptionText>
        </AgentSatus>
        ...
        ...
        ...
    </IssuingAgency>
</Child>
<Child>
    <submit>
        <application>
            ...
        </application>
    </submit>
    <IssuingAgency>
        <AgentIdentification>
            <IdentificationID>103701978</IdentificationID>
        </AgentIdentification>
        <AgentSequenceID>01</AgentSequenceID>
        <AgentSatus>
            <StatusText>R</StatusText>
            <StatusDescriptionText>Renewal</StatusDescriptionText>
        </AgentSatus>
        ...
        ...
        ...
    </IssuingAgency>
</Child>
<Child>
    <submit>
        <application>
            ...
        </application>
    </submit>
    <IssuingAgency>
            ...
        <AgentIdentification>
            <IdentificationID>103701978</IdentificationID>
        </AgentIdentification>
        <AgentSequenceID>01</AgentSequenceID>
        <AgentSatus>
            <StatusText>R</StatusText>
            <StatusDescriptionText>Renewal</StatusDescriptionText>
        </AgentSatus>
            ...
            ...
            ...
    </IssuingAgency>
</Child></Document>

现在我需要输出为:  output1.xml

<Document>
<Header>
    <Time>2011-04-22T10:57:00.000-04:00</Time>
    <UniqueId>103701978-01-ABC</UniqueId> 
</Header>
<Child>
    ...
    ... ALL Information should not be removed, they have to be retained 
    ...
    <IssuingAgency>
        <AgentIdentification>
            <IdentificationID>103701978</IdentificationID>
        </AgentIdentification>
        <AgentSequenceID>01</AgentSequenceID>
        <AgentSatus>
            <StatusText>ABC</StatusText>
            <StatusDescriptionText>Initial</StatusDescriptionText>
        </AgentSatus>
    </IssuingAgency>
    ...
    ...ALL Information should not be removed, they have to be retained
    ...

</Child></Document>

现在,如果观察到Header部分附加了Uinque Id,它是 Child / IssuingAgency / AgentIdentification / IdentificationID - Child / IssuingAgency / AgentSequenceID - Child / IssuingAgency / AgentSatus / StatusText的组合

同样,我需要三个不同的XML文件。生成的文件数量本质上是动态的,可以有10个子节点,因此输出可能是10个不同的文件,但标题信息添加了唯一的ID。

我尝试使用xslt生成不同的文件,但问题是我无法将Header信息与它们一起添加,我需要您的帮助才能解决此问题。如果快速解决,真的很感激。过去一周一直没有运气的尝试:(

1 个答案:

答案 0 :(得分:1)

假设XSLT 2.0,以下使用xsl:result-document和隧道参数:

<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:template match="/">
  <xsl:apply-templates select="Document/Child" mode="new-doc"/>
</xsl:template>

<xsl:template match="Child" mode="new-doc">
  <xsl:result-document href="output{position()}.xml">
    <xsl:apply-templates select="ancestor::*">
      <xsl:with-param name="current-child" select="current()" tunnel="yes"/>
    </xsl:apply-templates>
  </xsl:result-document>
</xsl:template>

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Child">
  <xsl:param name="current-child" tunnel="yes"/>
  <xsl:if test=". is $current-child">
      <xsl:copy-of select="."/>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>