English 中文(简体)
• 如何将XSL变革档案普遍化,用于所有情况
原标题:How to generalize XSL Transformation file to be used for all scenario
  • 时间:2012-04-24 11:23:11
  •  标签:
  • xml
  • xslt

如果我有这一xml档案:

    <root> 
    <node id="a">
        <Items id="a_1" method="pause">
            <item id="0" method="pause">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </Items>

        <Items id="a_1" method="pause">
            <item id="0" method="stop">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </Items>            
    </node>

    <node id="b">
        <Persons id="b_1">    
            <person id="b_1b" method="pause">
                <attribute>a</attribute>
            </person>       
            <person id="b_1a" method="start">
                <attribute>
                    <name>John</name>
                </attribute>
            </person>
            <person id="b_1b" method="stop">
                <attribute>a</attribute>
            </person>
            <person id="b_1a" method="pause">
                <attribute>a</attribute>
            </person>
        </Persons>

        <Persons id="b_1" method="start">               
            <person id="b_1a" method="stop">
                <attribute>a</attribute>
            </person>

            <person id="b_1b" method="start">
                <attribute>a</attribute>
            </person>
            <person id="b_1b" method="pause">
                <attribute>a</attribute>
            </person>
        </Persons>

        <Persons id="b_2">                
            <person id="b_1a" method="start">
                <attribute>
                    <name>John</name>
                </attribute>
            </person>
        </Persons>
    </node>
</root>

预计产出如下:

    <root> 
    <node id="a">
        <Items id="a_1" method="pause">

        </Items>

        <Items id="a_1" method="pause">
            <item id="0" method="stop">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </Items>            
    </node>

    <node id="b">
        <Persons id="b_1">    
        </Persons>

        <Persons id="b_1" method="start">               
            <person id="b_1a" method="stop">
                <attribute>a</attribute>
            </person>

            <person id="b_1b" method="start">
                <attribute>a</attribute>
            </person>
            <person id="b_1b" method="pause">
                <attribute>a</attribute>
            </person>
        </Persons>

        <Persons id="b_2">                
            <person id="b_1a" method="start">
                <attribute>
                    <name>John</name>
                </attribute>
            </person>
        </Persons>
    </node>
</root>

算法的关键在于stop方法。

  1. if it is the last remove all node before and leave the one with stop
  2. if node with stop method is not the last then remove that node with stop and all node before it (leave all node after that stop).

这必须发生在mong儿童身上,其父母的亲身特性是相同的<>/strong>,并且将只删除儿童节点(即使父母在删除使用者节点后是空洞的)。

For the example above:

  1. item id=0 pause then item id=0 stop -> the result will be item id=0 stop (parent: Items id=a_1-pause).
  2. person id=b_1a start then person id=b_1a pause then person id=b1_a stop (parents: Person id=b_1) - so the result become only person id=b_1a stop
  3. person id=b_1b pause then person id=b_1b stop then person id=b_1b start then person id=b_1b pause -> it will become person id=b_1b start then person id=b_1b pause (again we compare every person under the parent Person id=b_1; in the parent we dont care if one of them does not have method as long as it is the same id as shown on the example)

这是我只为人所知的。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <!-- Ignore person with  stop  method which are not the last such person -->
   <xsl:template match="person[@method= stop ][following::person[@method= stop ]]"/>

   <!-- Match other persons -->
   <xsl:template match="person">
      <!-- Copy the person if there isn t a following person with the same id and  stop  method -->
      <xsl:if test="not(following::person[@id=current()/@id][@method= stop ])">
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
      </xsl:if>
   </xsl:template>

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

Can it be changed to solve all other nodes (the node and parent name can be anything) ? Thanks very much.

kind regards, John

问题回答

必须在相同的父母中发生这种情况。

我想,这意味着你前面提到的制约因素只适用于兄弟般的节点。 但是,你以“0”为榜样,表明情况并非如此。 因此,我不理解上述条款。

<>Update: 这并不意味着“父母一方的子女”而是“父母亲人有相同价值的人”。

我迄今对具体内容的理解:

如果使用固定方法的成分在最后出现,那么与相同用户使用的其他方法(如停用和操作)的任何其他节点将予以删除。 但是,如果一个固定方法的要素不是最后的,那么该要素在停止之前就停止了自己和所有要素(用同样的id?) 页: 1

It seems to me that this can be summed up as:

如果存在一个带有停用方法的要素,那么该元素和任何具有相同名称和相同特性的前身元素将被删除。

如果该摘要不正确,请加以澄清。

Can it be changed to solve all other nodes (the node and parent name can be anything) ?

我没有想到只有模板能够做到这一点的途径。 但是,可以像你一样,用xsl:如果:

<xsl:template match="*">
  <!-- Copy the element if there isn t a following sibling element with
       the same name, same id and  stop  method, and if the element itself
       doesn t have a  stop  method. -->
  <xsl:if test="not(@method =  stop  or
                    following::*[local-name() = local-name(current()) and
                      @id = current()/@id and
                      ../@id = current()/../@id and
                      @method =  stop ])">
     <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
  </xsl:if>
</xsl:template>

我很想理解你对算法的描述,因此,如果上述结果不产生预期结果,请举一个具体例子,说明其结果与预期结果不同,并澄清具体内容。

<>Update: 我以最近的评论为基础,对上述准则进行了一些更新,但正如我现在理解的那样,这还不完全符合这一设想。 我将努力在一夜之间就此开展工作。 (其他人也受到欢迎!)





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml