0

源 XML:

<?xml version="1.0" encoding="UTF-8"?>
<BigData version="2.1" xmlns="bank.xsd">
<InsideData type="plan" name="testBANK" id="10">
<header>
  <log dateTime="2013-07-27T15:52:30"/>
</header>
<object class="BANK"  distName="CITY-1/ABC-1/BANK-1" operation="create"  timeStamp="2013-07-27T15:48:20"/>

<object class="BranchItemPeriod"  distName="CITY-1/ABC-1/BANK-1/Branch-1/BranchItem-1/BranchItemPeriod-1" operation="create"  timeStamp="2013-07-27T15:51:25">
  <p name="Week">0</p>
  <p name="interval">10</p>
</object>

<object class="BranchItemPeriod"  distName="CITY-1/ABC-1/BANK-1/Branch-1/BranchItem-2/BranchItemPeriod-2" operation="update"  timeStamp="2013-07-27T15:51:25">
  <p name="Week">0</p>
  <p name="interval">10</p>
</object>

<object class="Branch" distName="CITY-1/ABC-1/BANK-1/Branch-1" operation="create"  timeStamp="2013-07-27T15:48:31"/>

<object class="BranchItem"  distName="CITY-1/ABC-1/BANK-1/Branch-1/BranchItem-1" operation="create"  timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>

<object class="BranchItem"  distName="CITY-1/ABC-1/BANK-1/Branch-1/BranchItem-2" operation="update"  timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>

<object class="Sleep"  distName="CITY-1/ABC-1/Sleep-1" operation="create"  timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>
<object class="Dance"  distName="CITY-1/ABC-1/Dance-5" operation="create"  timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>

</InsideData>
</BigData>

转换 XSL:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:x="bank.xsd" exclude-result-prefixes="x">

<xsl:output encoding="UTF-8" indent="yes" method="xml"/>

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

<xsl:template match="x:object[@class = 'BANK' ]">
</xsl:template>

<xsl:template match="x:object[@class = 'Branch' ]">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="distName">
            <xsl:value-of select="concat(substring-before( @distName,'BANK-1/' ), substring-after( @distName, 'BANK-1/'))"/>
        </xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="x:object[@class = 'BranchItem' ]">
    <xsl:variable name="branchItem" select="."/>
    <xsl:choose>
        <xsl:when test="$branchItem/@operation='update' and not(contains($branchItem/@distName, 'JOBS_CREATED_USING_NE_LOCAL_UI'))">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:attribute name="operation">delete</xsl:attribute>
                <xsl:attribute name="distName">
                    <xsl:value-of select="concat(substring-before( @distName,'BANK-1/' ), substring-after( @distName, 'BANK-1/'))"/>
                </xsl:attribute>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:attribute name="operation">create</xsl:attribute>
                <xsl:attribute name="distName">
                    <xsl:value-of select="concat(substring-before( @distName,'BANK-1/' ), substring-after( @distName, 'BANK-1/'))"/>
                </xsl:attribute>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:attribute name="distName">
                    <xsl:value-of select="concat(substring-before( @distName,'BANK-1/' ), substring-after( @distName, 'BANK-1/'))"/>
                </xsl:attribute>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>

</xsl:template>

<xsl:template match="x:object[@class = 'BranchItemPeriod' ]">
    <xsl:variable name="branchPeiod" select="."/>
    <xsl:choose>
        <xsl:when test="$branchPeiod/@operation='update' and not(contains($branchPeiod/@distName, 'JOBS_CREATED_USING_NE_LOCAL_UI'))">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:attribute name="operation">create</xsl:attribute>
                <xsl:attribute name="distName">
                    <xsl:value-of select="concat(substring-before( @distName,'BANK-1/' ), substring-after( @distName, 'BANK-1/'))"/>
                </xsl:attribute>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:attribute name="distName">
                    <xsl:value-of select="concat(substring-before( @distName,'BANK-1/' ), substring-after( @distName, 'BANK-1/'))"/>
                </xsl:attribute>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

输出 XML:

<?xml version="1.0" encoding="UTF-8"?>
<BigData xmlns="bank.xsd" version="2.1">
<InsideData type="plan" name="testBANK" id="10">
<header>
  <log dateTime="2013-07-27T15:52:30"/>
</header>
<object class="BranchItemPeriod" distName="CITY-1/ABC-1/Branch-1/BranchItem-1/BranchItemPeriod-1" operation="create" timeStamp="2013-07-27T15:51:25">
  <p name="Week">0</p>
  <p name="interval">10</p>
</object>

<object class="BranchItemPeriod" distName="CITY-1/ABC-1/Branch-1/BranchItem-2/BranchItemPeriod-2" operation="create" timeStamp="2013-07-27T15:51:25">
  <p name="Week">0</p>
  <p name="interval">10</p>
</object>

<object class="Branch" distName="CITY-1/ABC-1/Branch-1" operation="create" timeStamp="2013-07-27T15:48:31"/>

<object class="BranchItem" distName="CITY-1/ABC-1/Branch-1/BranchItem-1" operation="create" timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>

<object class="BranchItem" distName="CITY-1/ABC-1/Branch-1/BranchItem-2" operation="delete" timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>
<object class="BranchItem" distName="CITY-1/ABC-1/Branch-1/BranchItem-2" operation="create" timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>

<object class="Sleep" distName="CITY-1/ABC-1/Sleep-1" operation="create" timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>
<object class="Dance" distName="CITY-1/ABC-1/Dance-5" operation="create" timeStamp="2013-07-27T15:50:42">
  <p name="openDate">2013-07-27</p>
  <p name="closeDate">2013-07-29</p>
</object>

</InsideData>
</BigData>

所需的输出 XML:

<?xml version="1.0" encoding="UTF-8"?>
<BigData xmlns="bank.xsd" version="2.1">
<InsideData type="plan" name="testBANK" id="10">
    <header>
        <log dateTime="2013-07-27T15:52:30"/>
    </header>

    <object class="Branch" distName="CITY-1/ABC-1/Branch-1" operation="create" timeStamp="2013-07-27T15:48:31"/>

    <object class="BranchItem" distName="CITY-1/ABC-1/Branch-1/BranchItem-1" operation="create" timeStamp="2013-07-27T15:50:42">
        <p name="openDate">2013-07-27</p>
        <p name="closeDate">2013-07-29</p>
    </object>

    <object class="BranchItemPeriod" distName="CITY-1/ABC-1/Branch-1/BranchItem-1/BranchItemPeriod-1" operation="create" timeStamp="2013-07-27T15:51:25">
        <p name="Week">0</p>
        <p name="interval">10</p>
    </object>

    <object class="BranchItem" distName="CITY-1/ABC-1/Branch-1/BranchItem-2" operation="delete" timeStamp="2013-07-27T15:50:42">
        <p name="openDate">2013-07-27</p>
        <p name="closeDate">2013-07-29</p>
    </object>

    <object class="BranchItem" distName="CITY-1/ABC-1/Branch-1/BranchItem-2" operation="create" timeStamp="2013-07-27T15:50:42">
        <p name="openDate">2013-07-27</p>
        <p name="closeDate">2013-07-29</p>
    </object>

    <object class="BranchItemPeriod" distName="CITY-1/ABC-1/Branch-1/BranchItem-2/BranchItemPeriod-2" operation="create" timeStamp="2013-07-27T15:51:25">
        <p name="Week">0</p>
        <p name="interval">10</p>
    </object>

    <object class="Sleep" distName="CITY-1/ABC-1/Sleep-1" operation="create" timeStamp="2013-07-27T15:50:42">
        <p name="openDate">2013-07-27</p>
        <p name="closeDate">2013-07-29</p>
    </object>
    <object class="Dance" distName="CITY-1/ABC-1/Dance-5" operation="create" timeStamp="2013-07-27T15:50:42">
        <p name="openDate">2013-07-27</p>
        <p name="closeDate">2013-07-29</p>
    </object>

</InsideData>
</BigData>

我可以实现大部分所需的输出,除了少数......

  1. 我希望根据对象节点的distName属性对输出进行排序。
  2. 我希望排序只发生在类名为BranchBranchItemBranchItemPeriod的某些子节点上。
  3. 在这里,我尝试使用删除创建操作进行更新,因此我还想保持删除创建的顺序,这也是我在当前转换逻辑中所做的,否则会发生这样的情况,即我首先根据上述标准对所有内容进行排序并应用其他转换逻辑。

非常感谢任何建议或帮助。

4

1 回答 1

1

我认为您需要的是与InsideData元素匹配的模板,然后您可以在其中按您需要的顺序选择子对象元素。

您首先要输出非“对象”元素,假设这些元素总是在对象元素之前。

 <xsl:apply-templates select="@*|node()[not(self::x:object)]"/>

然后,您将选择具有所需类属性的对象元素,并按您需要的顺序进行排序:

 <xsl:apply-templates select="x:object[@class='Branch' or @class='BranchItem' or @class='BranchItemPeriod']">
    <xsl:sort select="@distName"/>
 </xsl:apply-templates>

最后,您将输出具有属性的对象元素。

 <xsl:apply-templates select="x:object[not(@class='Branch' or @class='BranchItem' or @class='BranchItemPeriod')]"/>

尝试将此模板添加到您的 XSLT 中,看看您是如何开始的:

  <xsl:template match="x:InsideData">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()[not(self::x:object)]"/>
      <xsl:apply-templates select="x:object[@class='Branch' or @class='BranchItem' or @class='BranchItemPeriod']">
        <xsl:sort select="@distName"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="x:object[not(@class='Branch' or @class='BranchItem' or @class='BranchItemPeriod')]"/>
    </xsl:copy>
  </xsl:template>
于 2013-08-08T12:53:49.183 回答