Click here to Skip to main content
15,890,123 members
Home / Discussions / XML / XSL
   

XML / XSL

 
AnswerRe: Print XSLT? Pin
notarry27-May-09 16:06
notarry27-May-09 16:06 
QuestionReading XML Pin
M_Aurelius23-May-09 22:20
M_Aurelius23-May-09 22:20 
AnswerRe: Reading XML Pin
harold aptroot23-May-09 23:46
harold aptroot23-May-09 23:46 
QuestionHelp for newbie Pin
Konstantin Yazvinski21-May-09 12:28
Konstantin Yazvinski21-May-09 12:28 
AnswerRe: Help for newbie Pin
led mike22-May-09 4:31
led mike22-May-09 4:31 
QuestionSelect XmlElement Help Pin
papy-boom21-May-09 3:27
papy-boom21-May-09 3:27 
QuestionHow to create a tool to generate xslt to transform one xml to another? Pin
salon21-May-09 1:15
salon21-May-09 1:15 
Questionremoving unwanted empty XML elements (this one contains code in a legible form) Pin
Craig Irwin20-May-09 17:37
Craig Irwin20-May-09 17:37 
Any help on this one would be appreciated as it is doing my head in
As you will see by the "Current Output" I am generating unwanted translation tags containing the substring character generated in one of my for-each loops as it loops over my "Sample input"
I have a habit of getting this stuff inside out and back to front and am pretty sure the solution is staring me in the face in such an "obvious" fashion that I can't see it

The code I currently have
-----------------------------
<pre><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <xsl:output method="xml" indent="yes"/>

      <!-- declaring 2 variables to hold "from" and "to" values -->
      <xsl:variable name="from_what" select="Dictionary/@from"/>
      <xsl:variable name="to_what" select="Dictionary/@to"/>

      <xsl:template match="/">
            <!-- hard coded task2.xsd reference -->
            <Dictionary xsi:noNamespaceSchemaLocation="something.xsd" from="{$from_what}" to="{$to_what}">
                 
                  <!-- outer loop to get a sorted search list -->
                  <xsl:for-each select="//dictionaryEntry">
                        <xsl:sort select="search"/>
                       
                        <xsl:element name="translation">
                              <!-- substring the search string down to its
                                    first character for the "initial" attribute-->
                              <xsl:attribute name="initial">
                                    <xsl:value-of select="substring(search,1,1)"/>
                              </xsl:attribute>
                             
                        <xsl:for-each select="search[not(.=preceding::search)]">
                              <xsl:element name="search">
                                    <xsl:value-of select="."/>
                              </xsl:element>

                              <xsl:variable name="this_search" select="."/>
                              <xsl:for-each select="//dictionaryEntry[search=$this_search]">
                                    <xsl:for-each select="replace">
                                          <xsl:element name="replace">
                                                <xsl:value-of select="."/>
                                          </xsl:element>
                                    </xsl:for-each>
                              </xsl:for-each>
                        </xsl:for-each>
                        </xsl:element>
                  </xsl:for-each>              
            </Dictionary>
      </xsl:template>
</xsl:stylesheet>
</pre>


Current Output
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="something.xsd" from="nl" to="es">
   <translation initial="A">
         <search>A Clockwork Orange</search>
         <replace>La naranja mecánica</replace>
   </translation>
   <translation initial="A">
         <search>Ab Urbe Condita</search>
         <replace>Ab Urbe condita libri</replace>
         <replace>Ab urbe condita</replace>
   </translation>
   <translation initial="A"/>
   <translation initial="A">
         <search>Ab Urbe Condita jaartelling</search>
         <replace>Ab urbe condita</replace>
   </translation>
   <translation initial="B">
         <search>Batman and Robin</search>
         <replace>Batman y Robin</replace>
   </translation>
</Dictionary>


Sample Input
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:noNamespaceSchemaLocation="something.xsd"
                  from="nl"
                  to="es">
   <dictionaryEntry>
         <search>A Clockwork Orange</search>
         <replace>La naranja mecánica</replace>
   </dictionaryEntry>
   <dictionaryEntry>
         <search>Ab Urbe Condita</search>
         <replace>Ab Urbe condita libri</replace>
   </dictionaryEntry>
   <dictionaryEntry>
         <search>Ab Urbe Condita</search>
         <replace>Ab urbe condita</replace>
   </dictionaryEntry>
   <dictionaryEntry>
         <search>Ab Urbe Condita jaartelling</search>
         <replace>Ab urbe condita</replace>
   </dictionaryEntry>
   <dictionaryEntry>
         <search>Batman and Robin</search>
         <replace>Batman y Robin</replace>
   </dictionaryEntry>
</Dictionary>
AnswerRe: removing unwanted empty XML elements (this one contains code in a legible form) [modified] Pin
brifiction24-Apr-10 18:23
brifiction24-Apr-10 18:23 
QuestionRemoving unwanted empty XML elements Pin
Craig Irwin20-May-09 17:22
Craig Irwin20-May-09 17:22 
QuestionASP.Net : Xml Version Format Pin
Vijay Jadhav, India.18-May-09 0:18
Vijay Jadhav, India.18-May-09 0:18 
AnswerRe: ASP.Net : Xml Version Format Pin
Samer Aburabie18-May-09 7:29
Samer Aburabie18-May-09 7:29 
GeneralRe: ASP.Net : Xml Version Format Pin
Vijay Jadhav, India.18-May-09 22:01
Vijay Jadhav, India.18-May-09 22:01 
AnswerRe: ASP.Net : Xml Version Format [modified] Pin
George L. Jackson19-May-09 6:20
George L. Jackson19-May-09 6:20 
GeneralRe: ASP.Net : Xml Version Format Pin
Vijay Jadhav, India.20-May-09 0:55
Vijay Jadhav, India.20-May-09 0:55 
GeneralRe: ASP.Net : Xml Version Format Pin
Vijay Jadhav, India.25-May-09 20:03
Vijay Jadhav, India.25-May-09 20:03 
QuestionASP.NET : Identify XSD on the basis of XML in Web Service [modified] Pin
Vijay Jadhav, India.18-May-09 0:11
Vijay Jadhav, India.18-May-09 0:11 
Questionhow do i delete a xml file ? Pin
YiXiang_8916-May-09 22:47
YiXiang_8916-May-09 22:47 
Questionparse out the member name value Pin
califgal13-May-09 21:22
califgal13-May-09 21:22 
AnswerRe: parse out the member name value Pin
led mike14-May-09 4:36
led mike14-May-09 4:36 
QuestionParsing XML file -exception because element node contains a space in name Pin
califgal13-May-09 16:15
califgal13-May-09 16:15 
AnswerRe: Parsing XML file -exception because element node contains a space in name Pin
Samer Aburabie14-May-09 4:06
Samer Aburabie14-May-09 4:06 
GeneralRe: Parsing XML file -exception because element node contains a space in name Pin
Michael Dunn19-May-09 11:06
sitebuilderMichael Dunn19-May-09 11:06 
QuestionWriting an xml document Pin
dptalt12-May-09 5:55
dptalt12-May-09 5:55 
QuestionStoring binary data in xml file msxml and sending it to client through winsock Pin
hemlat11-May-09 20:27
hemlat11-May-09 20:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.