Jump to content

Martin Honnen

Members
  • Posts

    327
  • Joined

  • Last visited

Everything posted by Martin Honnen

  1. PHP's DOM implementation has http://www.php.net/manual/en/class.domcdatasection.php so with the DOM you should be able to find and distinguish CDATA section nodes. So pseudo code (sorry, I don't code with PHP) would be e.g. $table = $xml->getElementsByTagName("table")->item(0); for ($i = $table->childNodes->length - 1; i >= 0; i--) { $child = $table->childNodes->item(i); if ($child->nodeType == [b]XML_CDATA_SECTION_NODE) {[/b] $cdataWrapper = $xml->createElement("wrapper"); $cdataWrapper->textContent = $child->nodeValue; $child->parentNode->replaceChild($cdataWrapper, $child); } } Then in your stylesheet code you could match on e.g. table/wrapper.
  2. Sorry, but XSLT is not the right tool to deal with CDATA sections as the XSLT/XPath/XQuery data model does not know any CDATA sections so the stylesheet operates on a tree model with a single text node where the original markup in the source document has adjacent text and/or CDATA sections.If you need to manipulate CDATA section then you need a different data model and API, like DOM or like LINQ to XML.
  3. Here is an adapted stylesheet <xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:t="http://www.taleo.com/ws/integration/toolkit/2005/07"exclude-result-prefixes="t"><xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/><xsl:key name="distinctRecord" match="t:record" use="t:field[@name='clientjobid']" /><xsl:template match="/"><jobs> <xsl:for-each select="//t:record[generate-id(.) = generate-id(key('distinctRecord', t:field[@name='clientjobid'])[1])]"> <xsl:variable name="clientjobid" select="t:field[@name='clientjobid']" /> <job> <action><xsl:value-of select="t:field[@name='action']" /></action> <clientjobid><xsl:value-of select="$clientjobid" /></clientjobid> <customfield1> <xsl:for-each select="key('distinctRecord', $clientjobid)"> <xsl:if test="position() != 1">, </xsl:if> <xsl:value-of select="t:field[@name='customfield1']"/> </xsl:for-each> </customfield1> </job> </xsl:for-each></jobs></xsl:template></xsl:stylesheet>
  4. Well what kind of parameter is that, are you passing in a node-set? Or which type does that parameter have?The xml-to-string takes a node or node-set and serializes it to a string.And of course if you pass a parameter in then the code needs to reference that parameter somewhere to make use of it so assuming you have a node-set parameter you need e.g. <xsl:template match="/"> <xsl:call-template name="xml-to-string"> <xsl:with-param name="node-set" select="$ManualAlloc"/> </xsl:call-template></xsl:template>
  5. http://p2p.wrox.com/xslt/84026-setting-xml-cdata.html#post273275 has some sample code of applying the code I linked to.
  6. See http://lenzconsulting.com/xml-to-string/ for a pure XSLT solution or if you use a certain XSLT processor like Saxon use a supported extension function like http://www.saxonica.com/documentation/extensions/functions/serialize.xml.
  7. Here is a sample stylesheet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="sep" select="' '"/> <xsl:output method="text"/> <xsl:variable name="columns" select="/SALES/METADATA/DATA/@VALUE"/> <xsl:template match="/"> <xsl:apply-templates select="SALES/PERSON/DATA"/> </xsl:template> <xsl:template match="PERSON/DATA"> <xsl:apply-templates select="$columns"> <xsl:with-param name="data" select="."/> </xsl:apply-templates> <xsl:text></xsl:text> </xsl:template> <xsl:template match="METADATA/DATA/@VALUE"> <xsl:param name="data"/> <xsl:if test="position() > 1"> <xsl:value-of select="$sep"/> </xsl:if> <xsl:value-of select="$data/@*[local-name() = current()]"/> </xsl:template> </xsl:stylesheet> Output for the input <SALES><PERSON><DATA NAME="John" x="27"/><DATA NAME="Mary" x="67"/></PERSON><METADATA><DATA VALUE="x"/><DATA VALUE="NAME"/></METADATA></SALES> is 27 John67 Mary
  8. Here is some approach using XSLT 2.0 and for-each-group group-starting-with: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* , node()"/> </xsl:copy> </xsl:template> <xsl:template match="list"> <xsl:for-each-group select="listitem/*/node()" group-starting-with="note"> <xsl:choose> <xsl:when test="self::note"> <xsl:copy-of select="."/> <list continue="true"> <xsl:apply-templates select="current-group() except ."/> </list> </xsl:when> <xsl:otherwise> <list> <xsl:apply-templates select="current-group()"/> </list> </xsl:otherwise> </xsl:choose> </xsl:for-each-group> </xsl:template> <xsl:template match="para/text()"> <para> <xsl:value-of select="."/> </para> </xsl:template> </xsl:stylesheet> That stylesheet, when applied with an XSLT 2.0 processor like Saxon 9.3 outputs <?xml version="1.0" encoding="UTF-8"?><misc> <list> <para>some text..</para> <para>Here is some more text. </para> </list> <note> <para>This is my notice in a para..</para> </note> <list continue="true"> <para> find even more here ;-) </para> </list></misc> Does that help?I am afraid so far the problem is kind of underspecified, I don't know whether the list elements can contain anything other than para and note elements.
  9. What you have posted as the desired result is not a well-formed XML document as it lacks a single root element containing all other elements? Are you sure you want that kind of result?
×
×
  • Create New...