xml - Iterated concatenations and manually applied CDATA in XSLT -


thanks generous feedback members here, i've made significant progress on xml xml project.

that said, i'm stuck on 2 issues in final version of project , gain better understanding of why these issues occurring.

first need create concatenated element value made of multiple values. have working using following code:

<xsl:template match="estimate/jobparts/jobpart/description">     <description>         <xsl:value-of select="concat(estimate/description,'_qty-',estimate/jobparts/jobpart/qtyordered,'_',estimate/jobparts/jobpart/itemtemplate)"/>     </description> </xsl:template> 

this finds matches estimate/jobparts/jobpart/description, in cases uses first matching sibling element values when replacing description value.

would situation need use key manage iteration, , if so, how should go creating one? there way of handling using apply-template iteration management handled automatically, , if so, how constructed?

the other issue have need manually add cdata designation element , cannot add element list in cdata-section-elements because there element same name nested deeper in document structure not require cdata designation.

here code trying use manually add cdata flag:

<xsl:template match="estimate/description">     <description>         <xsl:text disable-output-escaping="yes">          &lt;![cdata[        </xsl:text>         <xsl:value-of select="estimate/description"/>         <xsl:text disable-output-escaping="yes">         ]]&gt;        </xsl:text>     </description> </xsl:template> 

interestingly has no effect whatsoever on target description element.

if has ideas why either (or both) of these transforms not working correctly, eager learn , understand.

apologies in advance barrage of code here original xml:

<?xml version="1.0"?> <podordersheet_main>      <estimate>         <customer>linfnc</customer>         <newjob>y</newjob>         <incrementjobversion>y</incrementjobversion>         <description><![cdata[409511]]></description>         <billpartstogether>1</billpartstogether>         <datesetup><![cdata[4/24/2013]]></datesetup>         <promisedate><![cdata[4/24/2013]]></promisedate>         <scheduledshipdate><![cdata[4/24/2013]]></scheduledshipdate>         <adminstatus>o</adminstatus>         <shipvia>1</shipvia>          <jobtype>5020</jobtype>         <ponum><![cdata[409511]]></ponum>         <itemtemplate><![cdata[33503mn_0212]]></itemtemplate>         <jobparts>             <jobpart>                 <jobpart>01</jobpart>                 <contactnum/>                 <description><![cdata[replacement of life insurance or annuities minnesota]]></description>                 <productionstatus>o</productionstatus>                 <qtyordered><![cdata[3]]></qtyordered>                 <shiptocontact/>                 <itemtemplate><![cdata[33503mn_0212]]></itemtemplate>                 <jobnotes>                     <jobnote>                         <department>001</department>                         <jobpart>01</jobpart>                         <note><![cdata[rush order]]></note>                     </jobnote>                 </jobnotes>             </jobpart>         </jobparts>     </estimate>      <estimate>         <customer>linfnc</customer>         <newjob>y</newjob>         <incrementjobversion>y</incrementjobversion>         <description><![cdata[409511]]></description>         <billpartstogether>1</billpartstogether>         <datesetup><![cdata[4/24/2013]]></datesetup>         <promisedate><![cdata[4/24/2013]]></promisedate>         <scheduledshipdate><![cdata[4/24/2013]]></scheduledshipdate>         <adminstatus>o</adminstatus>         <shipvia>1</shipvia>          <jobtype>5020</jobtype>         <ponum><![cdata[409511]]></ponum>         <itemtemplate><![cdata[al-fsc-fst068_z08]]></itemtemplate>         <jobparts>             <jobpart>                 <jobpart>01</jobpart>                 <contactnum/>                 <description><![cdata[american legacy iii c share fact sheet ]]></description>                 <productionstatus>o</productionstatus>                 <qtyordered><![cdata[1]]></qtyordered>                 <shiptocontact/>                 <itemtemplate><![cdata[al-fsc-fst068_z08]]></itemtemplate>                 <jobnotes>                     <jobnote>                         <department>001</department>                         <jobpart>01</jobpart>                         <note><![cdata[rush order]]></note>                     </jobnote>                 </jobnotes>             </jobpart>         </jobparts>     </estimate>      <estimate>         <customer>linfnc</customer>         <newjob>y</newjob>         <incrementjobversion>y</incrementjobversion>         <description><![cdata[409511]]></description>         <billpartstogether>1</billpartstogether>         <datesetup><![cdata[4/24/2013]]></datesetup>         <promisedate><![cdata[4/24/2013]]></promisedate>         <scheduledshipdate><![cdata[4/24/2013]]></scheduledshipdate>         <adminstatus>o</adminstatus>         <shipvia>1</shipvia>          <jobtype>5020</jobtype>         <ponum><![cdata[409511]]></ponum>         <itemtemplate><![cdata[an06819-al3c_1012]]></itemtemplate>         <jobparts>             <jobpart>                 <jobpart>01</jobpart>                 <contactnum/>                 <description><![cdata[american legacy iii c share application supplement  - multi-state]]></description>                 <productionstatus>o</productionstatus>                 <qtyordered><![cdata[1]]></qtyordered>                 <shiptocontact/>                 <itemtemplate><![cdata[an06819-al3c_1012]]></itemtemplate>                 <jobnotes>                     <jobnote>                         <department>001</department>                         <jobpart>01</jobpart>                         <note><![cdata[rush order]]></note>                     </jobnote>                 </jobnotes>             </jobpart>         </jobparts>     </estimate>  </podordersheet_main> 

and here desired output:

<?xml version="1.0" encoding="utf-8"?> <!doctype estimate> <estimate>     <customer>linfnc</customer>     <newjob>y</newjob>     <incrementjobversion>y</incrementjobversion>     <description><![cdata[409511]]></description>     <billpartstogether>1</billpartstogether>     <datesetup><![cdata[4/24/2013]]></datesetup>     <promisedate><![cdata[4/24/2013]]></promisedate>     <scheduledshipdate><![cdata[4/24/2013]]></scheduledshipdate>     <adminstatus>o</adminstatus>     <shipvia>1</shipvia>     <jobtype>5020</jobtype>     <ponum><![cdata[409511]]></ponum>     <itemtemplate><![cdata[33503mn_0212]]></itemtemplate>     <jobparts>         <jobpart>             <jobpart>01</jobpart>             <contactnum/>             <description>409511_qty-3_33503mn_0212</description>             <priority>1</priority>             <productionstatus>o</productionstatus>             <qtyordered><![cdata[3]]></qtyordered>             <shiptocontact/>             <itemtemplate><![cdata[33503mn_0212]]></itemtemplate>             <jobnotes>                 <jobnote>                     <department>001</department>                     <jobpart>01</jobpart>                     <note><![cdata[rush order]]></note>                 </jobnote>             </jobnotes>         </jobpart>         <jobpart>             <jobpart>02</jobpart>             <contactnum/>             <description>409511_qty-1_al-fsc-fst068_z08</description>             <priority>1</priority>             <productionstatus>o</productionstatus>             <qtyordered><![cdata[1]]></qtyordered>             <shiptocontact/>             <itemtemplate><![cdata[al-fsc-fst068_z08]]></itemtemplate>             <jobnotes>                 <jobnote>                     <department>001</department>                     <jobpart>02</jobpart>                     <note><![cdata[rush order]]></note>                 </jobnote>             </jobnotes>         </jobpart>         <jobpart>             <jobpart>03</jobpart>             <contactnum/>             <description>409511_qty-1_an06819-al3c_1012</description>             <priority>1</priority>             <productionstatus>o</productionstatus>             <qtyordered><![cdata[1]]></qtyordered>             <shiptocontact/>             <itemtemplate><![cdata[an06819-al3c_1012]]></itemtemplate>             <jobnotes>                 <jobnote>                     <department>001</department>                     <jobpart>03</jobpart>                     <note><![cdata[rush order]]></note>                 </jobnote>             </jobnotes>         </jobpart>     </jobparts> </estimate> 

and here xsl now:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output indent="yes" encoding="utf-8" method="xml"/>  <xsl:strip-space elements="*"/>  <xsl:output cdata-section-elements="datesetup promisedate scheduledshipdate ponum qtyordered itemtemplate note"/>  <xsl:template match="@*|node()">     <xsl:copy>         <xsl:apply-templates/>     </xsl:copy> </xsl:template>  <xsl:template match="/podordersheet_main">     <estimate>         <xsl:copy-of select="estimate[1]/customer"/>         <xsl:copy-of select="estimate[1]/newjob"/>         <xsl:copy-of select="estimate[1]/incrementjobversion"/>         <xsl:copy-of select="estimate[1]/description"/>         <xsl:copy-of select="estimate[1]/billpartstogether"/>         <xsl:copy-of select="estimate[1]/datesetup"/>         <xsl:copy-of select="estimate[1]/promisedate"/>         <xsl:copy-of select="estimate[1]/scheduledshipdate"/>         <xsl:copy-of select="estimate[1]/adminstatus"/>         <xsl:copy-of select="estimate[1]/shipvia"/>         <xsl:copy-of select="estimate[1]/jobtype"/>         <xsl:copy-of select="estimate[1]/ponum"/>         <xsl:copy-of select="estimate[1]/itemtemplate"/>         <jobparts>             <xsl:apply-templates select="estimate/jobparts/jobpart"/>         </jobparts>     </estimate> </xsl:template>  <xsl:template match="estimate/description"> <description>     <xsl:text disable-output-escaping="yes">      &lt;![cdata[    </xsl:text>         <xsl:value-of select="estimate/description"/>     <xsl:text disable-output-escaping="yes">     ]]&gt;    </xsl:text> </description> </xsl:template>  <xsl:template match="//estimate/jobparts/jobpart/description"> <description> <xsl:value-of select="concat(//estimate/description,'_qty-',//jobpart/qtyordered,'_',//jobpart/itemtemplate)"/> </description> </xsl:template>  <xsl:template match="jobpart">     <xsl:copy><xsl:number count="jobpart" level="any" format="01"/></xsl:copy> </xsl:template>  </xsl:stylesheet> 

many thanks...

there few problems stylesheet:

  • you including estimate/description element using xsl:copy-of. because of copy of element, , template matches estimate/description never applied transform it.

  • the location paths in match , select attributes can either absolute or relative context node. absolute location paths sign of poor stylesheet design, , make difficult pick out 1 of several nodes matching path. have discovered, set of nodes converted string taking string value of first node in set. rather messing picking single node set using position() somehow, best use relative localtion paths.

  • it seems unclear use of cdata, pcdata , entities. different ways of representing special characters in text nodes of xml, , meaning of, <![cdata[use <> in perl reading]]> , use &lt;&gt; in perl reading identical. since have 2 alternative ways of representing same text string, unlikely want 1 or other, possible have finicky piece of software demands xml data "just so".

this transform need. have changed initial xsl:copy list xsl:apply-templates children of first estimate element have no child elements themselves. way estimate/description template comes effect , cdata tag applied. jobpart/description template pulls 3 values required compound description string 3 xsl variables make things neater.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet     xmlns:xsl="http://www.w3.org/1999/xsl/transform"     version="1.0">      <xsl:output indent="yes" encoding="utf-8" method="xml"/>     <xsl:strip-space elements="*"/>      <xsl:output cdata-section-elements="datesetup promisedate scheduledshipdate ponum itemtemplate qtyordered note"/>      <xsl:template match="node()|@*">         <xsl:copy>             <xsl:apply-templates/>         </xsl:copy>     </xsl:template>      <xsl:template match="/podordersheet_main">         <estimate>             <xsl:apply-templates select="estimate[1]/*[not(*)]"/>             <jobparts>                 <xsl:apply-templates select="estimate/jobparts/jobpart"/>             </jobparts>         </estimate>     </xsl:template>      <xsl:template match="estimate/description">         <description>             <xsl:text disable-output-escaping="yes">&lt;![cdata[</xsl:text>             <xsl:value-of select="."/>             <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>         </description>     </xsl:template>      <xsl:template match="jobpart/description">         <xsl:variable name="estimate-description" select="ancestor::estimate/description"/>         <xsl:variable name="qty-ordered" select="parent::jobpart/qtyordered"/>         <xsl:variable name="item-template" select="parent::jobpart/itemtemplate"/>         <xsl:copy>             <xsl:value-of select="concat($estimate-description, '_qty-', $qty-ordered, '_', $item-template)"/>         </xsl:copy>     </xsl:template>      <xsl:template match="jobpart">         <xsl:copy><xsl:number count="jobpart" level="any" format="01"/></xsl:copy>     </xsl:template>  </xsl:stylesheet> 

output

<?xml version="1.0" encoding="utf-8"?> <estimate>    <customer>linfnc</customer>    <newjob>y</newjob>    <incrementjobversion>y</incrementjobversion>    <description><![cdata[409511]]></description>    <billpartstogether>1</billpartstogether>    <datesetup><![cdata[4/24/2013]]></datesetup>    <promisedate><![cdata[4/24/2013]]></promisedate>    <scheduledshipdate><![cdata[4/24/2013]]></scheduledshipdate>    <adminstatus>o</adminstatus>    <shipvia>1</shipvia>    <jobtype>5020</jobtype>    <ponum><![cdata[409511]]></ponum>    <itemtemplate><![cdata[33503mn_0212]]></itemtemplate>    <jobparts>       <jobpart>          <jobpart>01</jobpart>          <contactnum/>          <description>409511_qty-3_33503mn_0212</description>          <productionstatus>o</productionstatus>          <qtyordered><![cdata[3]]></qtyordered>          <shiptocontact/>          <itemtemplate><![cdata[33503mn_0212]]></itemtemplate>          <jobnotes>             <jobnote>                <department>001</department>                <jobpart>01</jobpart>                <note><![cdata[rush order]]></note>             </jobnote>          </jobnotes>       </jobpart>       <jobpart>          <jobpart>02</jobpart>          <contactnum/>          <description>409511_qty-1_al-fsc-fst068_z08</description>          <productionstatus>o</productionstatus>          <qtyordered><![cdata[1]]></qtyordered>          <shiptocontact/>          <itemtemplate><![cdata[al-fsc-fst068_z08]]></itemtemplate>          <jobnotes>             <jobnote>                <department>001</department>                <jobpart>02</jobpart>                <note><![cdata[rush order]]></note>             </jobnote>          </jobnotes>       </jobpart>       <jobpart>          <jobpart>03</jobpart>          <contactnum/>          <description>409511_qty-1_an06819-al3c_1012</description>          <productionstatus>o</productionstatus>          <qtyordered><![cdata[1]]></qtyordered>          <shiptocontact/>          <itemtemplate><![cdata[an06819-al3c_1012]]></itemtemplate>          <jobnotes>             <jobnote>                <department>001</department>                <jobpart>03</jobpart>                <note><![cdata[rush order]]></note>             </jobnote>          </jobnotes>       </jobpart>    </jobparts> </estimate> 

Comments