Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xsl:problems generating new xml from xml

Status
Not open for further replies.

werD420

Technical User
Sep 14, 2004
181
US
hello all im working on an xml/xsl tool that displays post left by management

it uses an xsl style to transform the xml containing my entries into an xml that can be quickly read into to my .net tools. here is the original xml

Code:
<data>
  <supervisors>
    <supentry 
      id="1" 
      author="Ray"
      attachment="1" 
      relevance="general" 
      date="8-24-06"><![CDATA[This is a test entry]]></supentry>
    <supentry 
      id="2" 
      author="Carl"
      attachment="none" 
      relevance="general" 
      date="8-24-06"><![CDATA[This is a second test entry]]></supentry>
    <supentry 
      id="3" 
      author="TheParrot"
      attachment="2" 
      relevance="general" 
      date="8-24-06"><![CDATA[testing still]]></supentry>
    <supentry 
      id="4" 
      author="joe"
      attachment="none" 
      relevance="general" 
      date="8-24-06"><![CDATA[This is a not test entry]]></supentry>
    <files>
      <file name="A Form" id="1" filename="holymountains.txt" />
      <file name="Another Form" id="2" filename="holyroller.txt" />
    </files>
  </supervisors>
  <teamleads>
    <tlentry
      id="1" 
      author="Bob"
      attachment="1" 
      relevance="specific"
      date="8-24-06"><![CDATA[This is a test entry]]></tlentry>
    <files>
      <file name="Formage" id="1" filename="holymountains.txt" />
      
    </files>
  </teamleads>

</data>

and here is my xsl to generate the new xml I seem to be having problems adding the attachemnt node with the correct attributes.
if the attachment node is "none" then it will just place a bookmark to that entry

Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]

<xsl:template match="/">
  <data>    
  <xsl:for-each select="data/supervisors/supentry">
        
          <entry>
            <id><xsl:value-of select="@id"/></id>
            <author>
              <xsl:value-of select="@author"/>
            </author>
            <comments><xsl:value-of select="." disable-output-escaping="yes" /></comments>
            <scope><xsl:value-of select="@relevance"/></scope>
            <attachment><xsl:attribute name="url">
              <xsl:call-template name="filename" />
              </xsl:attribute>
              <xsl:attribute name="text">
                <xsl:call-template name="filetitle" />
              </xsl:attribute>
            </attachment>

          </entry>
          </xsl:for-each>
  </data>

</xsl:template>
<!-- Filename Template displays attachemnt node with url and text attribute -->
  <xsl:template name="filename" match="/data/supervisors/files//file">
    <xsl:choose>
        <xsl:when test="@id =..//supentry/@attachment" >
          <xsl:if test="..//supentry/@attachment != 'none'">upload/<xsl:value-of select="..//file/@filename"/></xsl:if>
        </xsl:when>
        <xsl:otherwise>#SUP<xsl:value-of select="./@id"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="filetitle" match="/data/supervisors/files//file">
    <xsl:choose>
      <xsl:when test="@id =..//supentry/@attachment" >
        <xsl:if test="..//supentry/@attachment != 'none'">
          <xsl:value-of select="..//file/@name"/>
        </xsl:if>
      </xsl:when>
<xsl:otherwise>None</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>


I appreciate any help or direction i can get with this thanks
DrewG

MCP, .Net Solutions Development <%_%>
 
I ended up doing something a bit more logical for me

xml
Code:
<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet type="text/xsl" href="sup.xsl" ?>

<data>
  <supervisors>
    <supentry 
      id="1" 
      author="joe"
      relevance="general" 
      date="8-24-06"><![CDATA[This is a test entry]]>
      <file attachedto="1"  name="A Form" smid="" type="text" tags="" filename="holymountains.txt" />
      <file attachedto="1" name="Another Form" smid="" type="text" tags="" filename="holyroller.txt" />
    </supentry>
    </data>

xsl

Code:
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]

<xsl:template match="/">
  <data>    
  <xsl:for-each select="data/supervisors/supentry">
        
          <entry>
            <id><xsl:value-of select="@id"/></id>
            <author>
              <xsl:value-of select="@author"/>
            </author>
            <date>
              <xsl:value-of select="@date"/>
            </date>
            <comments><xsl:value-of select="." disable-output-escaping="yes" /></comments>
            <scope><xsl:value-of select="@relevance"/></scope>
            <xsl:if test="file/@filename != ''">
            <xsl:for-each select=".//file">
            
              <attachment>
                            
              <xsl:attribute name="url">
                <xsl:value-of select="@filename"/>
              </xsl:attribute>
                <xsl:attribute name="fileid">
                  <xsl:value-of select="@attachedto"/>
                </xsl:attribute>
              <xsl:attribute name="text">
                <xsl:value-of select="@name"/>
              </xsl:attribute>
              <xsl:attribute name="type">
                <xsl:value-of select="@type"/>
              </xsl:attribute>
              <xsl:attribute name="keywords">
                <xsl:choose>
                  <xsl:when test="@tags != ''">
                    <xsl:value-of select="@tags"/>
                  </xsl:when>
                  <xsl:otherwise>none</xsl:otherwise>
        </xsl:choose>
              </xsl:attribute>
            </attachment>
          </xsl:for-each>
            </xsl:if>
          </entry>
          </xsl:for-each>
  </data>

</xsl:template>
</xsl:stylesheet>




MCP, .Net Solutions Development <%_%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top