xsl-list
[Top] [All Lists]

Re: [xsl] xsl: parsing through specific child nodes

2008-09-06 00:08:49
Please find below the solution.

Let's say the two XML documents be:

x.xml

<ZZ>
  <ABC>XXXXX</ABC>
  <DEF>YYYYYY</DEF>
</ZZ>

y.xml

<ZZ>
  <ABC>AAAAA</ABC>
  <DEF>FFFFFFF</DEF>
</ZZ>

The stylesheet is:

test.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                       version="1.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:variable name="x" select="document('x.xml')" />
  <xsl:variable name="y" select="document('y.xml')" />

  <xsl:template match="/">
    <Body>
      <xsl:apply-templates select="$x/ZZ" />
      <xsl:apply-templates select="$y/ZZ" />
    </Body>
  </xsl:template>

  <xsl:template match="ZZ">
    <ZZ>
      <xsl:apply-templates select="*" />
    </ZZ>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:choose>
        <xsl:when test=". = 'YYYYYY'">
          <xsl:text>DDDDD</xsl:text>
        </xsl:when>
        <xsl:when test=". = 'GGGGGG'">
          <xsl:text>EEEEEE</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="." />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Supposing this is run with Xalan-J as:

java org.apache.xalan.xslt.Process -in test.xsl -xsl test.xsl

The output is:

<?xml version="1.0" encoding="UTF-8"?>
<Body>
  <ZZ>
    <ABC>XXXXX</ABC>
    <DEF>DDDDD</DEF>
  </ZZ>
  <ZZ>
    <ABC>AAAAA</ABC>
    <DEF>FFFFFFF</DEF>
  </ZZ>
</Body>


On Fri, Sep 5, 2008 at 10:47 PM, Mohit Anchlia 
<mohitanchlia(_at_)gmail(_dot_)com> wrote:
one more question, for above scenario should I be using xsl:key for
mapping purposes. for eg: If I want to change value from A to B for
certain node types?

I don't think xsl:key can be useful in this scenario.

-- 
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--