هل من الممكن تمرير معلمة إلى XSLT من خلال عنوان URL عند استخدام متصفح لتحويل XML؟

StackOverflow https://stackoverflow.com/questions/65926

سؤال

عند استخدام متصفح لتحويل XML (Google Chrome أو IE7) هل من الممكن تمرير معلمة إلى ورقة أنماط XSLT من خلال عنوان URL؟

مثال:

data.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<root>
    <document type="resume">
        <author>John Doe</author>
    </document>
    <document type="novella">
        <author>Jane Doe</author>
    </document>
</root>

Sample.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:output method="html" />
    <xsl:template match="/">
    <xsl:param name="doctype" />
    <html>
        <head>
            <title>List of <xsl:value-of select="$doctype" /></title>
        </head>
        <body>
            <xsl:for-each select="//document[@type = $doctype]">
                <p><xsl:value-of select="author" /></p>
            </xsl:for-each>
        </body>
    </html>
</<xsl:stylesheet>
هل كانت مفيدة؟

المحلول

يمكنك إنشاء جانب خادم XSLT، حتى لو كان التحويل من جانب العميل.

يتيح لك هذا استخدام برنامج نصي ديناميكي للتعامل مع المعلمة.

على سبيل المثال، يمكنك تحديد:

<?xml-stylesheet type="text/xsl"href="/myscript.cfm/sample.xsl?paramter=something" ?>

وبعد ذلك في myscript.cfm، يمكنك إخراج ملف XSL، ولكن باستخدام برنامج نصي ديناميكي يتعامل مع معلمة سلسلة الاستعلام (قد يختلف هذا اعتمادًا على لغة البرمجة النصية التي تستخدمها).

نصائح أخرى

لسوء الحظ، لا - لا يمكنك تمرير المعلمات إلى XSLT من جانب العميل فقط.يأخذ متصفح الويب تعليمات المعالجة من ملف XML؛ويحوله مباشرة باستخدام XSLT.


من الممكن تمرير القيم عبر عنوان URL لسلسلة الاستعلام ثم قراءتها ديناميكيًا باستخدام JavaScript.ومع ذلك، لن تكون هذه متاحة للاستخدام في XSLT (تعبيرات XPath) - حيث قام المتصفح بالفعل بتحويل XML/XSLT.ولا يمكن استخدامها إلا في مخرجات HTML المقدمة.

ما عليك سوى إضافة المعلمة كسمة إلى ملف مصدر XML واستخدامها كسمة مع ورقة الأنماط.

xmlDoc.documentElement.setAttribute("myparam",getParameter("myparam"))

ووظيفة جافا سكريبت هي كما يلي:

//Get querystring request paramter in javascript
function getParameter (parameterName ) {

   var queryString = window.top.location.search.substring(1);

   // Add "=" to the parameter name (i.e. parameterName=value)
   var parameterName = parameterName + "=";
   if ( queryString.length > 0 ) {
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
         end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
   }
   // Return "null" if no parameter has been found
   return "null";
   }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top