Selectively copy and update xml nodes using XSLT

前端 未结 1 1005
温柔的废话
温柔的废话 2021-01-29 08:18

I\'m working with an xml that I need to copy and update to pass on for further processing. The issue I\'m having is that I have not figured out an efficient method to do this. E

相关标签:
1条回答
  • 2021-01-29 09:04

    This should work for your original question:

    <xsl:stylesheet version="2.0" 
                    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
      <xsl:variable name='updateData' select='document("report")'/>
    
      <!-- Identity Transform -->
      <xsl:template match='@* | node()' name='copy'>
          <xsl:copy>
            <xsl:apply-templates select='@* | node()'/>
          </xsl:copy>
      </xsl:template>
    
      <xsl:template match='*[not(*)]'>
        <xsl:variable name='matchingValue' 
                      select='$updateData/*[name() = name(current())]'/>
        <xsl:choose>
          <xsl:when test='$matchingValue'>
            <xsl:copy>
              <xsl:apply-templates select='@*' />
              <xsl:value-of select='$matchingValue'/>
            </xsl:copy>
          </xsl:when>
          <xsl:when test='normalize-space()'>
            <xsl:call-template name='copy' />
          </xsl:when>
        </xsl:choose>
      </xsl:template>
    </xsl:stylesheet>
    

    As far as inserting new elements that are not present in the source XML, that's trickier. Could you open a separate question for that? I may have some ideas for how to approach that.

    0 讨论(0)
提交回复
热议问题