Jump to content

Martin Honnen

Members
  • Posts

    327
  • Joined

  • Last visited

Everything posted by Martin Honnen

  1. With XSLT 2.0 you can process any sequence so <xsl:for-each select="1 to 5">...</xsl:for-each> processes a sequence of five integers 1,2,3,4,5. Make sure you store the input document you want to process outside of the for-each in a variable if you want to access it inside of the for-each as the context item is the currently processed integer number.
  2. Assuming you have an XSLT 2.0 processor like Saxon 9 or XmlPrime or Exselt or Altova and you want to split up a string into a sequence of strings where each string has a certain size then [ <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mf="http://example.com/mf" exclude-result-prefixes="xs mf" version="2.0"> <xsl:function name="mf:split" as="xs:string*"> <xsl:param name="input" as="xs:string"/> <xsl:param name="size" as="xs:integer"/> <xsl:for-each-group select="string-to-codepoints($input)" group-adjacent="(position() - 1) idiv $size"> <xsl:sequence select="codepoints-to-string(current-group())"/> </xsl:for-each-group> </xsl:function> <xsl:template match="/" name="main"> <xsl:value-of select="mf:split('123456789012345678901234567890123', 5)" separator=" "/> </xsl:template> </xsl:stylesheet> outputs 12345 67890 12345 67890 12345 67890 123
  3. Which XSLT processor do you use, which XSLT version? There are no arrays in XSLT 1.0 and 2.0, so your whole question about "splitting into an array" sounds odd. Unless you work with XPath 3.1 where arrays exist but then you need to tell so.
  4. I only know Saxon 9 which comes in various editions, the open source Home Edition HE should suffice for your needs to use XSLT and XPath 2.0, at least in terms of support of the specification. There are also the commercial Personal Edition PE and the Enterprise Edition EE for which one has to buy a license. I don't know of any other Java XSLT 2.0 implementation, other than that IBM Websphere package I already mentioned. Some things you want to do like getting the current date could be done with Xalan by making use of parameters or by making use of extension functions calling into Java.
  5. Yes, I think so, the current-date() function was introduced in XSLT/XPath 2.0 which is not supported by Xalan as it is an XSLT 1.0 processor.
  6. If you want to use XSLT and XPath 2.0 with Java then the built-in XSLT processor Xalan does not work as it is an XSLT 1.0 processor, you will need to switch to Saxon 9 from http://saxon.sourceforge.net/ (or if you use IBM Websphere you can check with them, I think they provide XSLT 2.0 as a package as well).
  7. Which XSLT 2.0 processor do you use? Don't you get some more detailed information like the line number in your code where the error occurs?
  8. XML started without a concept of namespaces but it got added soon. And nowadays most of XML is processed in a namespace-aware mode where names with colons are allowed but have a special meaning, see http://en.wikipedia.org/wiki/XML_namespace. So normally an XML documents looks like <html xmlns="http://www.w3.org/1999/xhtml">...</html> or <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">...</xsl:stylesheet> As for XPath and XSLT and XQuery, they all operate on XML with namespaces. If you have a namespace well-formed XML input document like <root> <prefix:name xmlns:prefix="http://example.org/">foo</prefix:name></root> then your XSLT needs to declare the namespace as well e.g. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:prefix="http://example.org/"> <xsl:template match="root"> <xsl:value-of select="prefix:name"/></xsl:template> </xsl:stylesheet> If you still have problems, post your XML sample.
  9. Your code looks fine to me. Any chance your real XML has some <output xmlns="http://example.org">...</output> ?
  10. Change <xsl:for-each select="parameters/param"> to <xsl:for-each select="parameters/param |document('file2.xml')/parameters/param">
  11. When compiling your stylesheet, your XSLT processor should tell you that the `xsl:with-param` is not allowed where you put it. For instance with <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="root"> <xsl:apply-templates select="foo"/> <xsl:with-param name="p" select="1"/></xsl:template></xsl:stylesheet> Saxon 6.5.5 tells me Error at xsl:with-param on line 7 of file:/C:/Users/Foo/Bar/sheet.xsl xsl:with-param cannot appear as a child of xsl:template
  12. It sounds a bit as if you want to define a schema for your class of XML documents and then validate XML instance documents with a validating parser against the schema.
  13. A SAX parser can do that: http://www.saxproject.org/apidoc/org/xml/sax/EntityResolver.html. XSLT does not have DTDs or entities in its data model so it is not suitable to solve your problem.
  14. Given the XML you have the "a" elements are children of the "p" elements so you would need to use <xsl:for-each select="rss/channel/item/description/p[2]"><img src="{a/@href}" style="margin:5px 5px" /></xsl:for-each>
  15. Write a template <xsl:template match="*:ExportXML"> <JobPositionPostings> <xsl:apply-templates/> </JobPositionPostings></xsl:template> and remove the element from the other template, that is change <xsl:template match="*:record"> <JobPositionPostings> <JobPositionPosting> <xsl:apply-templates select="*:field[starts-with(@name,'ContestNumber')]"/> <JobDisplayOptions> <xsl:apply-templates select="*:field[starts-with(@name,'ManagerRequisitionTitle')]"/> </JobDisplayOptions> </JobPositionPosting> </JobPositionPostings> </xsl:template> to <xsl:template match="*:record"> <JobPositionPosting> <xsl:apply-templates select="*:field[starts-with(@name,'ContestNumber')]"/> <JobDisplayOptions> <xsl:apply-templates select="*:field[starts-with(@name,'ManagerRequisitionTitle')]"/> </JobDisplayOptions> </JobPositionPosting> </xsl:template>
  16. Can you show us a sample of the XML input you want to transform? That way it is easier to make suggestions on how to transform it. Currently with <xsl:template match="*:record"> <JobPositionPostings> <JobPositionPosting> you are mapping each "*:record" element to a "JobPositionPostings" element. If that is not your objective then you need to change the code so that the "JobPositionPostings" element is only created once. Usually you do that by creating it in a template matching a parent or ancestor of "*:record" and doing apply-templates. So assuming the parent element is named "*:record-list" you would use <xsl:template match="*:record-list"> <JobPositionPostings> <xsl:apply-templates/> </JobPositionPostings></xsl:template>
  17. It is called a "result tree fragment". Also note that XSLT 2.0 exists since 2007 and that there you can have temporary trees instead of result tree fragments. Furthermore XSLT 2.0 has an "if (condition) then expression1 else expression2" expression. So with an XSLT 2.0 processor like Saxon 9 you can avoid such problems.
  18. Put the two templates <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy></xsl:template> <xsl:template match="Field/text()"> <xsl:attribute name="value"> <xsl:value-of select="."/> </xsl:attribute></xsl:template> into a stylesheet and it should work.
  19. Well show us your XML input format.Assuming you have e.g. <root> <images> <image>img1.gif</image> <image>img2.gif</image> </images></root> in your XML you can then transform to HTML with e.g. <xsl:template match="images"> <ul> <xsl:apply-templates/> </ul></xsl:template> <xsl:template match="image"> <li> <img src="{.}"/> </li></xsl:template>
  20. You don't need for-each at all I think, you can simply use <xsl:value-of select="sum(Gather/Stat/Point[. < 0])"/> If that does not help then show a sample of the XML you have.
  21. With XSLT 2.0: <xsl:value-of select="distinct-values(//sample/@name/substring-after(., '_'))"/> With XSLT 1.0 you can use Muenchian grouping: <xsl:key name="k1" match="sample/@name" use="substring-after(., '_')"/> <xsl:template match="/"> <xsl:apply-templates select="//sample/@name[generate-id() = generate-id(key('k1', substring-after(., '_'))[1])]"/></xsl:template> <xsl:template match="sample/@name"> <xsl:if test="position() > 1"><xsl:text> </xsl:text></xsl:if> <xsl:value-of select="substring-after(., '_')"/></xsl:template>
  22. Use the "test" attribute on xsl: when, not the "select" attribute <xsl:when test="nomeaula/@aula='RED'"></xsl:when> See http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:choose for the syntax.
  23. Javascript/ECMAScript has two types of comments, single line ones starting with // this is a comment and multiple line ones /*This is a commenton several lines*/ The code <!-- --> is not a Javascript comment, however Javascript code is often embedded inline in HTML or XHTML documents where <!-- --> are comment delimiters.As for code inside such comments not being executed, well with <!--<script>window.location = 'foo.html';</script>--> the whole script element is commented out in the surrounding HTML and that way the script code is ignored and not executed.On the other hand you might find <script><!--window.location = 'foo.html';//--></script> That approach dates back from the ancient past in terms of web development when there were browser not knowing and supporting the "script" element and others supporting it; to ensure that the browsers not supporting the "script" element did not render the script code as text on the page you would comment out the contents of the "script" element and the browsers supporting the "script" element would execute the embedded code although it was commented out. This approach is however no longer needed and not recommended in my view.
  24. So the problem is with different origins of files and security restrictions in browsers. Inside the browser you will find that script in HTML documents loaded from the file system might be denied access to other origins where depending on the browser and depending on security settings a different origin could be a HTTP server or a different directory on the file system. Or script in a document loaded from on HTTP domain can't access the file system or can't access a different HTTP domain (although the latest versions of browsers usually allow that if the server cooperates, see http://en.wikipedia.org/wiki/Cross-origin_resource_sharing). You might want to ask in a forum dedicated to the browser you target or at least post in http://w3schools.invisionzone.com/index.php?showforum=26, explaining in detail the locations of files used when you get those errors, I don't think this is XSLT related.
  25. It is not clear why you mix XSLT and HTML and Javascript. If you want to load an additional XML document in your XSLT code then use the "document" function with e.g. <xsl:variable name="doc2" select="document('cd_catalog.xml')"/> If on the other hand you simply want to use Javascript in HTML, why do you also need XSLT?As for the Javascript not working, tell us exactly which browsers you have tried and which error messages you see in the error or script console of the browser. So far the only problem I see is that Mozilla browsers do not support the use of document.write in script in the result of an XSLT transformation. But that way you shouldn't get any output.
×
×
  • Create New...