在属性值内输出php处理指令

时间:2017-06-06 15:47:17

标签: html xslt xslt-2.0 processing-instruction

在我的XSLT(2.0 - 输出方法是html)中我有这个:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class LoginSample {
    private JFrame frame;
    private JLabel userLabel;
    private JLabel passLabel;
    private JTextField userField;
    private JPasswordField passField;
    private JButton button;
    private JButton button2;
    private File file;
    private JPanel pane;
    private JPanel contentPane;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LoginSample().createAndShowGui();
        });
    }

    private void createAndShowGui() {
        GridBagConstraints gbc = new GridBagConstraints();

        frame = new JFrame(getClass().getSimpleName());
        contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        userLabel = new JLabel("User: ");
        passLabel = new JLabel("Password: ");
        userField = new JTextField(10);
        passField = new JPasswordField(10);

        pane = new JPanel();
        pane.setLayout(new GridLayout(1, 2));
        pane.add(userLabel);
        pane.add(userField);

        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.gridx = 0;
        gbc.gridy = 0;

        contentPane.add(pane, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        pane = new JPanel();
        pane.setLayout(new GridLayout(1, 2));
        pane.add(passLabel);
        pane.add(passField);
        contentPane.add(pane, gbc);

        button = new JButton("Click me!");
        button2 = new JButton("Do not click me");

        file = new File("src/sof/users.txt");
        System.out.println(file.getAbsolutePath());

        button.addActionListener(listener);
        button2.addActionListener(listener);

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

        gbc.gridy = 3;
        contentPane.add(button2, gbc);

        frame.add(contentPane);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private ActionListener listener = e -> {
        if (e.getSource().equals(button)) {
            String s = "";
            Scanner sc = null;
            try {
                sc = new Scanner(file);
                while (sc.hasNext()) {
                    s = sc.nextLine();
                    System.out.println(s);
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } finally {
                sc.close();
            }
            String[] parts = s.split("\\.");
            String user = parts[0].trim();
            String pass = parts[1].trim();
            System.out.println(user + " <<>> " + Arrays.toString(pass.toCharArray()));
            System.out.println(userField.getText() + " <<>> " + Arrays.toString(passField.getPassword()));
            System.out.println(user.equals(userField.getText()));
            System.out.println(Arrays.equals(pass.toCharArray(), passField.getPassword()));
        } else {
            System.out.println("Told you not to click me! You made me cry :'(");
        }
    };
}

我想要的输出如下:

<img>
    <xsl:attribute name="href">
        <xsl:text disable-output-escaping="yes">&lt;?php echo get_url(); ?&gt;</xsl:text>
    </xsl:attribute>
</img>

我得到的输出如下:

<img href="<?php echo get_url(); ?>">

尝试了很多不同的东西来获得“&gt;”在输出中出现而不是&amp; gt; (CDATA标记的部分等)但似乎没有任何作用。奇怪的是,不到标志工作正常,但大于没有。我正在使用Saxon-PE 9.5.1.7。

1 个答案:

答案 0 :(得分:0)

使用带有其他地方不需要的字符的字符映射,这是一个根据XSLT 2.0规范改编的示例(https://www.w3.org/TR/xslt20/#character-maps):

RewriteRule ^(.) encyclopedia?letter=$1&term=%{REQUEST_URI} [NE,L]

<img href="«?php echo get_url(); ?»"/>

在线示例位于http://xsltransform.net/93dEHFP

至于disable-output-escaping,据我所知,它在属性值中不起作用,你得到的结果不是disable-output-escaping的结果,而只是<xsl:output method="html" use-character-maps="m1"/> <xsl:character-map name="m1"> <xsl:output-character character="«" string="&lt;"/> <xsl:output-character character="»" string="&gt;"/> </xsl:character-map> 的使用( https://www.w3.org/TR/xslt-xquery-serialization/#HTML_ATTRIBS)强制要求'HTML输出方法绝不能逃脱'&lt;“属性值中出现的字符。'。