xsl-list
[Top] [All Lists]

XSLT Merge question

2003-06-26 07:24:02
Here is something I've been trying to work out.  I need an XSLT that
will take a struts.xml file, find the matching tags in another xml
file, and if it finds a match replace it with the other xml tags.  I've
got this part working, the kicker is that if there are any additional
tags in the existing XML file that these must still be kept.  i.e. if
the an action tag in the struts xml file has 6 forward tags with it and
it's matching tag in the other file only has two, the two forward tags
must be copied in, and the other 4 forward tags must be kept.   I know
I'm close, but have run into a wall.  

Here is the base XSLT I'm using to copy the external action tag into
the new XML file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml"/>
   <xsl:template match="/">
      <xsl:apply-templates />
   </xsl:template>
   
   <xsl:template 
     match="*|@*|comment()|processing-instruction()|text()">
       <xsl:choose>
          <xsl:when test="action != ''">
             <xsl:apply-templates select="action"/>
          </xsl:when>
       <xsl:otherwise>
        <xsl:copy>
         <xsl:apply-templates
          select="*|@*|comment()|processing-instruction()|text()"/>
         </xsl:copy>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
   
   <xsl:template match="action">
      <xsl:choose>
      <xsl:when test="@path =
document('struts-config_new_variables01.xml')//action/@path">
      <!-- Copy the value where @path = document()//action/@path -->
          <xsl:variable name="pathvalue"><xsl:value-of select="@path"
/></xsl:variable>
          <xsl:copy-of
select="document('struts-config_new_variables01.xml')//action[(_at_)path =
$pathvalue]"/>
      </xsl:when>
      <xsl:otherwise>
         <xsl:copy-of select="." />
      </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

</xsl:stylesheet>


Here is a sample of the tags I'm trying to match from the struts.xml
file:

<action path="/CopytoUpdate"
    type = "cmsapplication.application.web.CopytoUpdateAction"
    name="copytoCopytoForm" 
    scope="request" 
    input="/CopytoMaintChange.jsp?altura.action=Update">
    <forward name="success" path="/CopytoStore.do" redirect="false" />
    <forward name="failure"
path="/cmsCopytoMaintChange.jsp?altura.action=Update" redirect="false"
/>
    <forward name="contextError" path="/CopytoBrowse.do"
redirect="false" />
    <forward name="CopytoDelete" path="/CopytoDelete.do"
redirect="false" />
    <forward name="CopytoStore" path="/CopytoStore.do" redirect="false"
/>
    <forward name="validationfailure" path="/CopytoSelect.do"
redirect="false" />
    <!-- Use Default Component for SelectList: Grpcpt -->
    <forward name="GrpcptSelectList" path="/GrpcptSelectList.do" />
    <forward name="RemoveCopytoGrpcpt"
path="/CopytoGrpcptRemoveDown.do" />
    <!-- Use Default Component for SelectList: State -->
    <forward name="StateSelectList" path="/StateSelectList.do" />
    <forward name="StateRemoveUp" path="/CopytoStateRemoveUp.do" />
    <!-- Use Default Component for SelectList: Country -->
    <forward name="CountrySelectList" path="/CountrySelectList.do" />
    <forward name="CountryRemoveUp" path="/CopytoCountryRemoveUp.do" />
</action>

Here is the external file that I need to use an update for the Action
tag:
<action path="/CopytoUpdate"
    type = "cmsapplication.application.web.CopytoUpdateAction"
    name="copytoCopytoForm" 
    scope="request" 
    input="/cmsCopytoMaintChange.jsp?altura.action=Update">

    <forward name="failure"
path="cmsCopytoMaintChange.jsp?altura.action=Update" redirect="false"
/>
    <forward name="validationfailure"
path="cmsCopytoMaintChange.jsp?altura.action=Update" redirect="false"
/>
</action>

What I need to happen is to update the Action tag, and the two
forwards, but also keep the other forwards that aren't in the external
file.  I've tried switching to the following xslt, but I'm not getting
the results I thought I would (i've used XML Spys debugger to step
through it).

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml"/>
   <xsl:template match="/">
      <xsl:apply-templates />
   </xsl:template>
   
   <xsl:template 
     match="*|@*|comment()|processing-instruction()|text()">
       <xsl:choose>
          <xsl:when test="action != ''">
             <xsl:apply-templates select="action"/>
          </xsl:when>
       <xsl:otherwise>
        <xsl:copy>
         <xsl:apply-templates
          select="*|@*|comment()|processing-instruction()|text()"/>
         </xsl:copy>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
   
   <xsl:template match="action">
      <xsl:choose>
      <xsl:when test="@path =
document('struts-config_new_variables01.xml')//action/@path">
      <!-- Copy the value where @path = document()//action/@path -->
          <xsl:variable name="pathvalue"><xsl:value-of select="@path"
/></xsl:variable>
          <xsl:copy-of
select="document('struts-config_new_variables01.xml')//action[(_at_)path =
$pathvalue]"/>
      </xsl:when>
      <xsl:otherwise>
         <xsl:copy>
             <xsl:apply-templates
          select="*|@*|comment()|processing-instruction()|text()"/>
         </xsl:copy>

      </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
   
   <xsl:template match="forward">
        <xsl:copy>
         <xsl:apply-templates
          select="*|@*|comment()|processing-instruction()|text()"/>
         </xsl:copy>
   </xsl:template>

</xsl:stylesheet>




=====
"The difference between fiction and reality is that fiction has to make
sense." - Tom Clancy

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>