在特定位置添加元素

时间:2016-09-27 09:55:13

标签: xml xslt

我有一个XML,希望在特定位置插入ENTRY

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"?>
<TABLE FRAME="ALL">
<TGROUP COLS="3">
<COLSPEC COLNAME="col1" COLWIDTH="5cm"/>
<COLSPEC COLNAME="col2" COLWIDTH="6.00cm"/>
<COLSPEC COLNAME="col3" COLWIDTH="5.91cm"/>
<THEAD>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA><EMPHASIS>TA</EMPHASIS></PARA></ENTRY>
<ENTRY COLSEP="1" ROWSEP="1"><PARA><EMPHASIS>TB</EMPHASIS></PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA><EMPHASIS>TC</EMPHASIS></PARA></ENTRY>
</ROW>
</THEAD>
<TBODY>
<ROW>
<ENTRY COLSEP="1" MOREROWS="13" ROWSEP="1"><PARA>A</PARA></ENTRY>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>B</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>C</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A1</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>B1</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A2</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>B2</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A4</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A5</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A5<PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A6</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A6</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A7</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" MOREROWS="20" ROWSEP="1"><PARA>A7</PARA></ENTRY>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A8</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>B4</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A8</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A9</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A9</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A10</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A11</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A12</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A12</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A13</PARA></ENTRY>
</ROW>
</TBODY>
</TGROUP>
</TABLE>

XSLT版本1.0

<xsl:stylesheet>
  <xsl:template match="TBODY/ROW[position() > 1]">

    <xsl:element name="{name()}">
      <xsl:apply-templates select="@*"/>
      <xsl:element name="ENTRY"/>
      <xsl:apply-templates />
    </xsl:element>

  </xsl:template>
<xsl:stylesheet>

期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"?>
<TABLE FRAME="ALL">
<TGROUP COLS="3">
<COLSPEC COLNAME="col1" COLWIDTH="5cm"/>
<COLSPEC COLNAME="col2" COLWIDTH="6.00cm"/>
<COLSPEC COLNAME="col3" COLWIDTH="5.91cm"/>
<THEAD>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA><EMPHASIS>TA</EMPHASIS></PARA></ENTRY>
<ENTRY COLSEP="1" ROWSEP="1"><PARA><EMPHASIS>TB</EMPHASIS></PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA><EMPHASIS>TC</EMPHASIS></PARA></ENTRY>
</ROW>
</THEAD>
<TBODY>
<ROW>
<ENTRY COLSEP="1" MOREROWS="13" ROWSEP="1"><PARA>A</PARA></ENTRY>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>B</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>C</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A1</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>B1</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A2</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>B2</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A4</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A5</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A5<PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A6</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A6</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A7</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" MOREROWS="20" ROWSEP="1"><PARA>A7</PARA></ENTRY>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A8</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>B4</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A8</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A9</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A9</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A10</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A11</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A12</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY/>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A12</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A13</PARA></ENTRY>
</ROW>
</TBODY>
</TGROUP>
</TABLE>

如果检测到MOREROWS,请离开第一行并从第二行插入ENTRY

MOREROWS用于跨越单元格。

enter image description here

1 个答案:

答案 0 :(得分:0)

参考上述评论,请查看以下代码(注意:我使用的是用于快速演示的depricated API,用相应的API替换相同的代码):

package xml;

import java.io.FileInputStream;
import java.io.FileWriter;

import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class InsertChildTest {

    public static void main(String[] args) throws Exception{

        FileInputStream fis=new FileInputStream("E:\\workspaces\\dev_werewolf\\GeneralTests\\XML_Questions\\ATemplate.xml");

        InputSource inputSource=new InputSource(fis);

        DOMParser parser=new DOMParser();
        parser.parse(inputSource);

        Document document=parser.getDocument();

        NodeList questionList=document.getDocumentElement().getElementsByTagName("Question");

        Element someElement=document.createElement("SomeElement");
        document.getDocumentElement().insertBefore(someElement, questionList.item(1));

        OutputFormat outputFormat=new OutputFormat();
        XMLSerializer serializer=new XMLSerializer(new FileWriter("E:\\workspaces\\dev_werewolf\\GeneralTests\\XML_Questions\\ATemplate_Modified.xml"), outputFormat);

        serializer.serialize(document);

    }//main closing

}//class closing

输入文件是:

<?xml version="1.0" encoding="UTF-8"?>
<Questionnaire identifier="questionnaire_id">

    <Question slNo="1" type="closedEnded" remarks="true" singleSelect="true">
        <QuestionText>what is this?</QuestionText>
        <Options>
            <Option caption="option1" value="45-56" weightage="4"/>
            <Option caption="option2" value="85-906" weightage="10"/>
            <Option caption="option1" value="None of the above" weightage="5"/>
        </Options>
        <Answers>
            <Option caption="option1" value="45-56" weightage="4"/>
        </Answers>
    </Question>

    <Question slNo="2" type="openEnded" remarks="false">
        <QuestionText>What is that?</QuestionText>
        <Answers>
            <Text>what is what!? I do not understand</Text>
        </Answers>
    </Question>

    <Question slNo="3" type="mixed" remarks="false" singleSelect="false">
        <QuestionText>what is the other thing?</QuestionText>
        <Options>
            <Option caption="option4" value="Bad" weightage="9"/>
            <Option caption="option5" value="somewhat good" weightage="10"/>
            <Option caption="option6" value="extremely bad" weightage="10"/>
        </Options>
        <Answers>
            <Option caption="option4" value="Bad" weightage="9"/>
            <Option caption="option5" value="somewhat good" weightage="10"/>
            <Option caption="option6" value="extremely bad" weightage="10"/>
            <Text>what is what!? I do not understand</Text>
        </Answers>
    </Question>

</Questionnaire>

输出文件是(注意在第二个问题之前插入的SomeElement):

<?xml version="1.0" encoding="UTF-8"?>
<Questionnaire identifier="questionnaire_id">

    <Question remarks="true" singleSelect="true" slNo="1" type="closedEnded">
        <QuestionText>what is this?</QuestionText>
        <Options>
            <Option caption="option1" value="45-56" weightage="4"/>
            <Option caption="option2" value="85-906" weightage="10"/>
            <Option caption="option1" value="None of the above" weightage="5"/>
        </Options>
        <Answers>
            <Option caption="option1" value="45-56" weightage="4"/>
        </Answers>
    </Question>

    <SomeElement/><Question remarks="false" slNo="2" type="openEnded">
        <QuestionText>What is that?</QuestionText>
        <Answers>
            <Text>what is what!? I do not understand</Text>
        </Answers>
    </Question>

    <Question remarks="false" singleSelect="false" slNo="3" type="mixed">
        <QuestionText>what is the other thing?</QuestionText>
        <Options>
            <Option caption="option4" value="Bad" weightage="9"/>
            <Option caption="option5" value="somewhat good" weightage="10"/>
            <Option caption="option6" value="extremely bad" weightage="10"/>
        </Options>
        <Answers>
            <Option caption="option4" value="Bad" weightage="9"/>
            <Option caption="option5" value="somewhat good" weightage="10"/>
            <Option caption="option6" value="extremely bad" weightage="10"/>
            <Text>what is what!? I do not understand</Text>
        </Answers>
    </Question>

</Questionnaire>