简体   繁体   English

XSL XML:添加模式弹出窗口

[英]XSL XML: adding a modal popup

Here is the xml: 这是xml:

<?xml version="1.0" encoding="UTF-8"?>
<file>
    <text>
        <title>Header1</title>
        <content>
            <p>
                <sentence>I bought <fruit>kiwi</fruit> at the grocery store.</sentence>
                <sentence>I also bought <fruit>bananas</fruit> at the store.</sentence>
            </p>
            <p>
                <sentence>She bought <fruit>kiwi</fruit> at the grocery store.</sentence>
                <sentence>She also bought <fruit>bananas</fruit> at the store.</sentence>
            </p>
        </content>
        <footer>Footer1</footer>
    </text>
    <text>
        <title>Header2</title>
        <content>
            <p>
                <sentence>He bought <fruit>pears</fruit> at the grocery store.</sentence>
                <sentence>He also bought <fruit>lemons</fruit> at the store.</sentence>
            </p>
            <p>
                <sentence>You bought <fruit>pears</fruit> at the grocery store.</sentence>
                <sentence>You also bought <fruit>lemons</fruit> at the store.</sentence>
            </p>
        </content>
        <footer>Footer2</footer>
    </text>
</file>

I'm trying to add a modal popup that appears when clicking on a <sentence> and display the entire <content> in the modal popup along with the header and footer. 我正在尝试添加一个在单击<sentence>时出现的模式弹出窗口,并在模式弹出窗口中显示整个<content>以及页眉和页脚。 I have the following xsl that doesn't display any <sentence> s after adding Modal Content <div> s. 添加模态内容<div>之后,我的以下xsl不会显示任何<sentence> Also, how can I pass a unique ID to Modal Content, so that it displays the correct <content> where the <sentence> belongs? 另外,如何将唯一的ID传递给模式内容,以便显示<sentence>所属的正确<content>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:template match="/">
                    <xsl:apply-templates select="file/text/content/p"/>
    </xsl:template>

<xsl:template match="fruit">
<span style="color:red;">
<xsl:apply-templates/>
</span>
</xsl:template>

<xsl:template match="sentence[fruit]">
<p id="myBtn" onclick="myBtn()">
<xsl:apply-templates/>
</p>

<!-- The Modal -->
<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">&times;</span>
<h2>
<xsl:value-of select="file/text/title"/>
</h2>
</div>
<div class="modal-body">
<p><xsl:value-of select="file/text/content"/>
</p>
</div>
<div class="modal-footer">
<h3>
<xsl:value-of select="file/text/footer"/>
</h3>
</div>
</div>

<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the sentence, open the modal
btn.onclick = function() {
modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>

</xsl:template>

</xsl:stylesheet>

When using an XPath such as 当使用诸如

select="file/text/title"

You're saying "from the current element, select its child file, from here its child text, and from here its child title". 您说的是“从当前元素中,选择其子文件,从此处选择其子文本,并从此处选择其子标题”。

But file is not a child of sentence, so you're not selecting anything. 但是文件不是句子的子元素,因此您没有选择任何内容。

You need to select "the title element which is in the same text element as the current element". 您需要选择“与当前元素位于同一文本元素中的标题元素”。 And that is 那就是

select="ancestor::text/title"

Also, how can I pass a unique ID to Modal Content 另外,如何将唯一ID传递给模式内容

You could leverage generate-id(): 您可以利用generate-id():

id="{generate-id()}"

or you could count how many sentences you've been through so far: 或者,您可以算出到目前为止已经阅读了多少句话:

id="modal{count(preceding::sentence)}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM