Click here to Skip to main content
14,361,747 members
   

XML / XSL

 
QuestionHow to create XSD without repeating myself Pin
Mikael Sterley28-Apr-16 4:46
memberMikael Sterley28-Apr-16 4:46 
AnswerRe: How to create XSD without repeating myself Pin
George Jonsson13-Jun-16 4:08
professionalGeorge Jonsson13-Jun-16 4:08 
AnswerRe: How to create XSD without repeating myself Pin
Bernhard Hiller5-Sep-16 22:08
memberBernhard Hiller5-Sep-16 22:08 
Questiongenerating a unique value and adding it to a group of elements Pin
Drew Church27-Mar-16 15:11
memberDrew Church27-Mar-16 15:11 
AnswerRe: generating a unique value and adding it to a group of elements Pin
Jim Meadors27-Mar-16 19:29
memberJim Meadors27-Mar-16 19:29 
GeneralRe: generating a unique value and adding it to a group of elements Pin
Drew Church28-Mar-16 3:07
memberDrew Church28-Mar-16 3:07 
GeneralRe: generating a unique value and adding it to a group of elements Pin
Jim Meadors28-Mar-16 20:44
memberJim Meadors28-Mar-16 20:44 
GeneralRe: generating a unique value and adding it to a group of elements Pin
Drew Church29-Mar-16 4:26
memberDrew Church29-Mar-16 4:26 
Thank you Jim for your help but I think I solved the problem. I am a programmer and believe me I would have had this done in C# or VB sprinkle in some LINQ in less than an hour if I had my way. Loading it into Excel does work and is a valid solution, I just wanted to try to solve it with XSLT. I also need to automate this for everyday mail merges so Access is a nice easy solution for the company I work for. So below is my code:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
 
  <!--Identity template, provides default behavior that copies all content into the output -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
   <!--Add a root element-->
   <xsl:template match="/">
        <dataroot>
            <xsl:apply-templates select="@*|node()"/>
        </dataroot>
    </xsl:template>
        
  <!--match an envelope node and create an id element with a unique identifier-->
  <xsl:template match="envelope">
    <xsl:copy>
        <xsl:element name="id">
          <xsl:value-of select="generate-id(.)"/> 
        </xsl:element>
      <xsl:apply-templates select="@*|node()">
          <xsl:with-param name="prev_id" select="generate-id()"/>
        </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!--match a person element, create an element named id and copy the unique value that was created in the parent envelope element into the id element-->
  <xsl:template match="person">
    <xsl:param name="prev_id"/>
    <xsl:copy>
      <xsl:variable name="cur_id">
        <xsl:value-of select="$prev_id"/>/>
      </xsl:variable>
      <xsl:element name="id"><xsl:value-of select="$cur_id"/></xsl:element>
      <xsl:apply-templates select="@*|node()">
        <xsl:with-param name="prev_id" select="$cur_id"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  
  <!--match a contact element, create an element named id and copy the unique value that was created in the parent envelope element into the id element-->
  <xsl:template match="contact">
    <xsl:param name="prev_id"/>
    <xsl:copy>
      <xsl:variable name="cur_id">
        <xsl:value-of select="$prev_id"/>/>
      </xsl:variable>
      <xsl:element name="id"><xsl:value-of select="$cur_id"/></xsl:element>
      <xsl:apply-templates select="@*|node()">
        <xsl:with-param name="prev_id" select="$cur_id"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!--match a address element, create an element named id and copy the unique value that was created in the parent envelope element into the id element-->
  <xsl:template match="address">
    <xsl:param name="prev_id"/>
    <xsl:copy>
      <xsl:variable name="cur_id">
        <xsl:value-of select="$prev_id"/>/>
      </xsl:variable>
      <xsl:element name="id"><xsl:value-of select="$cur_id"/></xsl:element>
      <xsl:apply-templates select="@*|node()">
        <xsl:with-param name="prev_id" select="$cur_id"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!--match a notice element, create an element named id and copy the unique value that was created in the parent envelope element into the id element-->
  <xsl:template match="notice">
    <xsl:param name="prev_id"/>
    <xsl:copy>
      <xsl:variable name="cur_id">
        <xsl:value-of select="$prev_id"/>/>
      </xsl:variable>
      <xsl:element name="id"><xsl:value-of select="$cur_id"/></xsl:element>
      <xsl:apply-templates select="@*|node()">
        <xsl:with-param name="prev_id" select="$cur_id"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

GeneralRe: generating a unique value and adding it to a group of elements Pin
Jim Meadors29-Mar-16 20:12
memberJim Meadors29-Mar-16 20:12 
Questiontheory xml . help me Pin
Member 1216400424-Nov-15 6:29
memberMember 1216400424-Nov-15 6:29 
AnswerRe: theory xml . help me Pin
George Jonsson4-Dec-15 21:45
professionalGeorge Jonsson4-Dec-15 21:45 
QuestionXML parsing error --> #10; Pin
NJdotnetdev14-Jul-15 3:23
memberNJdotnetdev14-Jul-15 3:23 
AnswerRe: XML parsing error --> #10; Pin
Vinod Jangle5-Aug-15 3:27
memberVinod Jangle5-Aug-15 3:27 
QuestionNETIQ Product Help needed. Pin
Member 1156144127-Mar-15 11:24
memberMember 1156144127-Mar-15 11:24 
AnswerRe: NETIQ Product Help needed. Pin
Richard Andrew x6427-Mar-15 15:14
professionalRichard Andrew x6427-Mar-15 15:14 
AnswerRe: NETIQ Product Help needed. Pin
Abhipal Singh19-May-15 6:07
professionalAbhipal Singh19-May-15 6:07 
Questioncopying attribute from one node to other Pin
ramina sen9-Feb-15 1:10
memberramina sen9-Feb-15 1:10 
QuestionXSL: how to properly reference to xml field? Pin
Maciej Los16-Nov-14 10:05
protectorMaciej Los16-Nov-14 10:05 
AnswerRe: XSL: how to properly reference to xml field? Pin
Richard Deeming17-Nov-14 3:52
mveRichard Deeming17-Nov-14 3:52 
GeneralRe: XSL: how to properly reference to xml field? Pin
Maciej Los17-Nov-14 5:25
protectorMaciej Los17-Nov-14 5:25 
QuestionHow to get behavior similar to the obsoleted XmlValidatingReader Pin
John Whitmire30-Oct-14 4:53
professionalJohn Whitmire30-Oct-14 4:53 
AnswerRe: How to get behavior similar to the obsoleted XmlValidatingReader Pin
Gerry Schmitz30-Oct-14 22:21
mveGerry Schmitz30-Oct-14 22:21 
GeneralRe: How to get behavior similar to the obsoleted XmlValidatingReader Pin
John Whitmire3-Nov-14 4:04
professionalJohn Whitmire3-Nov-14 4:04 
SuggestionRe: How to get behavior similar to the obsoleted XmlValidatingReader Pin
John Whitmire6-Nov-14 3:47
professionalJohn Whitmire6-Nov-14 3:47 
QuestionVB.NET creat new XML Pin
Dereak12-Oct-14 18:43
memberDereak12-Oct-14 18:43 

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.