Ryan Jensen
Ryan Jensen

Reputation: 101

Issue with XSLT conversion

I'm trying to take this source and transform it with the following rules:
1. The number of Entry elements will vary, so it must be a for-each loop and 2. Only get the Entry Elements that contain CATDV

<?xml version="1.0"?>
<Metadata>
  <Entry>
    <Tag>CatDV 1</Tag>
    <Value>CCC</Value>
  </Entry>
  <Entry>
    <Tag>CatDV 2</Tag>
    <Value>DDD</Value>
  </Entry>
  <Entry>
    <Tag>Something Else</Tag>
    <Value>EEE</Value>
  </Entry>
  <Entry>
    <Tag>CatDV 3</Tag>
    <Value>FFF</Value>
  </Entry>
</Metadata>

I've tried this but I can't seem to get it to narrow down to just get the values that have the tag CATDV.

  <catdv version="2.0">
    <remoteID>
      <xsl:choose>
        <xsl:when test="/Metadata/Entry[Tag = 'CATDV RemoteID']">
          <xsl:value-of select="/Metadata/Entry[Tag = 'CATDV RemoteID']/Value"/>
        </xsl:when>
      </xsl:choose>
    </remoteID>
    <metadata>
      <xsl:choose>
        <xsl:when test="Metadata/Entry/Tag = 'CATDV Animal'">
          <xsl:for-each select="Metadata/Entry">
            <field>
              <xsl:value-of select="/Metadata/Entry/Value"/>
            </field>
          </xsl:for-each>
        </xsl:when>
      </xsl:choose>
    </metadata>
  </catdv>

Ultimately I'm wanting this result.

<catdv version="2.0">
    <metadata>
        <fieldname>CATDV 1</fieldname>
        <fieldvalue>CCC</fieldvalue>
        <fieldname>CatDV 2</fieldname>
        <fieldvalue>>DDD</fieldvalue>
        <fieldname>CatDV 3</fieldname>
        <fieldvalue>FFF</fieldvalue>
    </metadata>
</catdv>

Upvotes: 0

Views: 60

Answers (4)

Stefan Hegny
Stefan Hegny

Reputation: 2177

I would do it like this

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

<xsl:transform version="1.0"  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
    <catdv version="2.0">
        <metadata>
            <xsl:apply-templates select="Metadata/Entry[contains(Tag,'CatDV')]"/>
        </metadata>
    </catdv>
</xsl:template>

<xsl:template match="Entry">
    <fieldname><xsl:value-of select="Tag"/></fieldname>
    <fieldvalue><xsl:value-of select="Tag"/></fieldvalue>

but there may be other ways - Stefan

Upvotes: 0

zx485
zx485

Reputation: 29022

A simple XSLT-1.0 solution is this:

<?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:template match="/Metadata"> 
    <catdv version="2.0">
      <metadata>
        <xsl:apply-templates select="Entry[contains(Tag,'CatDV')]" />
      </metadata>
    </catdv>
  </xsl:template>
  <xsl:template match="Entry"> 
    <fieldname><xsl:value-of select="Tag" /></fieldname>
    <fieldvalue><xsl:value-of select="Value" /></fieldvalue>
  </xsl:template>
</xsl:stylesheet>

Its output is as desired:

<?xml version="1.0"?>
<catdv version="2.0">
  <metadata>
    <fieldname>CatDV 1</fieldname>
    <fieldvalue>CCC</fieldvalue>
    <fieldname>CatDV 2</fieldname>
    <fieldvalue>DDD</fieldvalue>
    <fieldname>CatDV 3</fieldname>
    <fieldvalue>FFF</fieldvalue>
  </metadata>
</catdv>

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 116959

How about simply;

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/Metadata">
    <catdv version="2.0">
        <metadata>
            <xsl:for-each select="Entry[contains(Tag, 'CatDV')]">
            <fieldname>
                <xsl:value-of select="Tag" />
            </fieldname>
            <fieldvalue>
                <xsl:value-of select="Value" />
            </fieldvalue>
            </xsl:for-each>
        </metadata>
    </catdv>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

flyx
flyx

Reputation: 39638

<xsl:for-each select="Metadata/Entry">
  <xsl:if test="contains(Tag, 'CatDV')">
    <field>
      <xsl:value-of select="Value"/>
    </field>
  </xsl:if>
</xsl:for-each>

If you want to make the comparison case-insensitive, look here.

Upvotes: 0

Related Questions