在XSL Transformation中订购节点

时间:2013-08-08 12:13:27

标签: xslt xslt-1.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>

所需的OUTPUT 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. 我希望排序只发生在类名为分支 BranchItem BranchItemPeriod 的某些子节点上。
  3. 此处我尝试使用删除创建操作更新,因此我还希望维持删除的顺序我现在在转换逻辑中执行strong>和创建,否则就会发生这种情况,我首先根据上述标准对所有内容进行排序并应用其他转换逻辑。
  4. 非常感谢任何建议或帮助。

1 个答案:

答案 0 :(得分: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>