如何使用xsl将父标签与嵌套标签一起使用

时间:2019-06-15 09:03:36

标签: xslt-2.0

我有一个看起来像这样的XML文件:

public class MainFrame extends javax.swing.JFrame {

    private static final long serialVersionUID = 8971599546866585146L;
    JButton loadDir = new JButton("LOAD DIR");
    JButton runHDrize = new JButton("RUN Converter");
    JButton saveHDR = new JButton("SAVE HDR");
    JScrollBar inputScroller = new JScrollBar(JScrollBar.HORIZONTAL);
    JPanel curve = initCurve();
    JPanel mapping = initMapping();
    JPanel lambda = initLambda();
    JPanel samples = initSamples();

    public MainFrame() {
        this.setLayout(new GridBagLayout());
        this.setSize(800, 700);
        this.setResizable(false);
        initFrame();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void initFrame() {
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        add(inputScroller, gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        add(saveHDR, gbc);
        saveHDR.addActionListener(new OutputDialogListener(null));  //TODO Get image

        gbc.gridx = 0;
        gbc.gridy = 2;
        add(curve, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        add(samples, gbc);
        samples.setVisible(true);

        gbc.gridwidth = 1;
        gbc.gridx = 3;
        gbc.gridy = 2;
        add(lambda, gbc);
        lambda.setVisible(true);

        gbc.gridx = 0;
        gbc.gridy = 3;
        add(mapping, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        add(loadDir, gbc);
        loadDir.addActionListener(new InputDialogListener());

        gbc.gridx = 3;
        gbc.gridy = 3;
        add(runHDrize, gbc);
    }

    private JPanel initCurve() {
        Code to initialize a JCombobox to choose which camera curve to use
    }

    private JPanel initMapping() {
        Code to initialize a JCombobox with a description field over it to select the mapping used for conversion from HDR to RGB
    }

    private JPanel initLambda() {
        Code to initialize a field turning red when an invalid value is entered

    }

    private JPanel initSamples() {
        Code to initialize a slider with field showing current value above
    }
}

必需的输出:

<SNS>
<SN>aaaa</SN>
</SNS>
<SNS>
<SN>bbbb</SN>
<LN>cccc</LN>
</SNS>
<SNS>
<SN>dddd</SN>
</SNS>
<SNS>
<SN>eeee</SN>
<LN>ffff</LN>
</SNS>

如何在每个“ SN”标签中添加“ LN”?

1 个答案:

答案 0 :(得分:0)

首先,提供所提供的XML is invalid。它必须包含在根元素中。例如:

<root>
  <SNS>
    <SN>aaaa</SN>
  </SNS>
  <SNS>
    <SN>bbbb</SN>
    <LN>cccc</LN>
  </SNS>
  <SNS>
    <SN>dddd</SN>
  </SNS>
  <SNS>
    <SN>eeee</SN>
    <LN>ffff</LN>
  </SNS>
</root>

然后,如果要输出 只有<SNS>元素的后代,可以使用以下转换:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="SNS//*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>