1

I want to use some kind of XML filtering, using javascript w/ jQuery. I'm not very familiar with xslt yet. I've seen that there's always a XML document and a certain style (XSL) for that document. Then, the result is rendered in XHTML. I really just want to grab some XML, filter (by node name, some attribute, etc) and generate a (filtered/smaller) version of that xml. Do you think the xslt approach is the simplest ?

Thanks in advance

3
  • If the input is valid XML (passes a parser), then yes, XSLT is quite handy for transforming XML documents. Commented Oct 24, 2011 at 10:39
  • 1
    this may help you johannburkard.de/software/xsltjs Commented Oct 24, 2011 at 10:44
  • Thanks for the comment. I've already seen that lib, but unfortunately it lacks of proper documentation and examples :( Commented Oct 24, 2011 at 10:55

2 Answers 2

2

I really just want to grab some XML, filter (by node name, some attribute, etc) and generate a (filtered/smaller) version of that xml. Do you think the xslt approach is the simplest ?

XSLT is a language that has been especially designed for transformation of tree-structured input. This is why it is probably the best and simplest for implementing such tasks.

Here is an example:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

We want to produce from this XML document another document that has the same structure and element name/content, but contains only those num elements from the original document, whose valu is multiple of 3.

Here is the transformation to accomplish this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="num[not(. mod 3) = 0]"/>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer! How can I configure dynamically my transformation, inside javascript, for example passing variables to this transform ? Thank you once again
@jose: Passing parameters to a transformation is implementation dependent and one must read the documentation describing how to do this for the particular XSLT processor being used. This is different for MSXML4 and XslCompiledTransform, and, ...
1

If you want to process an XML document and return another XML document, the XSLT approach is probably best.

If you only want to pull out a few nodes from your XML and display them, the built-in javascript functionality should be sufficient.

var title = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;

var lang = xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.