簡體   English   中英

如何將CSS格式應用於XSL表單輸入字段

[英]How can I apply CSS formating to an XSL form input field

因此,我在創建MADLIB類型故事的網頁上有這個復雜的小問題。 這些故事采用XML格式,看起來像這樣:

<?xml version="1.0"?>
  <body>
       I remember going to sleep around <Num1 class="Number">9</Num1> O'clock.
       In my dream, I was laying on a <N1 class="Noun">table</N1> in a
       strange room with no <PN1 class="Plural Noun">windows</PN1> or doors.
  </body>

使用和XSL工作表將“ body”標記的所有子代(例如“ Num1”,“ N1”和“ PN1”)處理為HTML表單,如下所示:

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

<xsl:template match="/body">

<form method="post" action="Results.php" style="text-align:right;margin-top: 75px;margin-right:25%;">
  <xsl:for-each select="child::*">
            <label>
                <xsl:value-of select="@class"/>: 
                <input name="{name()}" type="text" />
            </label>
            <br />

<input type="submit" value="Submit" name="submitButton"/>
  </xsl:for-each>  

</form>
</xsl:template>
</xsl:stylesheet>

由此,創建一個表單,該表單顯示基於子元素的類的標簽以及表單數據的輸入框。 就像是:

號碼:_________
名詞:_________
復數名詞: _________

提交

假設我為“數字”輸入25,為“名詞”輸入Frog,為“復數名詞”輸入Trees。

輸入並提交表單數據后,將通過PHP處理表單數據以替換原始故事中的子節點字符串。

<html>
  <head><link rel="stylesheet" type="text/css" href="SFJPlaza.css"></head>
<body>

  <div id="story">

    <?php

      $file = $_POST[filepath];
      $open = fopen($file, 'r');
      $theData = fread($open, filesize($file));
      fclose($open);

      $doc = DOMDocument::loadXML($theData, LIBXML_NOERROR);
      if ($doc !== FALSE) {
          $text = ''; // used to accumulate output while walking XML tree
          foreach ($doc->documentElement->childNodes as $child) {
             if ($child->nodeType == XML_TEXT_NODE) { // keep text nodes
                 $text .= $child->wholeText;
             } else if (array_key_exists($child->tagName, $_POST)) {
                 // replace nodes whose tag matches a POST variable
                 $text .= $_POST[$child->tagName];
             } else { // keep other nodes
                 $text .= $doc->saveXML($child);
             }
   }
//print $text . "\n";
} else {
    echo "Failed to parse XML\n";
}

        $xml = new DOMDocument();
        $root = $xml->createElement("body");
        $xml->appendChild($root);

        $bodyText = $xml->createTextNode($text);
        $root->appendChild($bodyText);

        //$xml->formatOutput = true;
        print "<pre>".$xml->saveXML() ."</pre>";
    ?>
 </div>
</body>
</html>

顯示故事時,它將顯示為:

“我記得要在25點左右睡覺。在我的夢中,我躺在一個沒有樹或門的陌生房間里的青蛙上。”

所有這些都很好,很好,很花哨。 不過,我想做的是分別格式化表單數據中的字段-25,Frog和Trees。 這樣,我可以為它們添加下划線,顏色或粗體樣式,以使其在故事的其余部分中脫穎而出,成為替換詞。

有人知道我該怎么做嗎?

在此先感謝Mutant先生

代替for-each select="*"使用apply-templates select="*"然后為您擁有的不同元素編寫模板,例如

<xsl:template match="Num1">
  <label style="color: yellow">... </label>
</xsl:template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM