0

I need to modifies the XML, I have a parent tag parts which contain child tag part and Child tag sub child tags, They are Item, brand, Manufacturer, Model, Cost. And it have a Brands tag and it contain Tag name called Brand. I need to change the brand attribute (identifier) value. If the Brand child tag Contain (textcontent) text (Geforce_GT) is equal to the text in part tag : brand child tag : text (Geforce_GT). Then Id attribute value of brand tag in part tag should assign to the Identifeir attribute in Brand tag child of Brands Parent tag.

<?xml version="1.0" ?>
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css" ?>
<PARTS>
  <TITLE>Computer Parts</TITLE>
  <PART>
    <ITEM id="CP1809_E1">Motherboard</ITEM>
    <MANUFACTURER>ASUS</MANUFACTURER>
    <MODEL>P3B-F</MODEL>
    <COST>123.00</COST>
  </PART>
  <PART>
    <ITEM id="CP1809_E2">Video Card</ITEM>
    <BRAND id="CP1809_B1">Geforce_GT</BRAND>
    <MANUFACTURER>ATI</MANUFACTURER>
    <MODEL>All-in-Wonder Pro</MODEL>
    <BRAND id="CP1809_B2">730_64-BIT</BRAND>
    <COST>160.00</COST>
  </PART>
  <PART>
    <ITEM id="CP1809_E3">Sound Card</ITEM>
    <MANUFACTURER>Creative Labs</MANUFACTURER>
    <MODEL>Sound Blaster Live</MODEL>
    <COST>80.00</COST>
  </PART>
  <PART>
    <ITEM id="CP1809_E3">inch Monitor</ITEM>
    <MANUFACTURER>LG Electronics</MANUFACTURER>
    <MODEL>995E</MODEL>
    <COST>290.00</COST>
  </PART>
  <BRANDS>
    <BRAND identifier="CP1809_E2">
      <TEXTCONTENT>Geforce_GT</TEXTCONTENT>
    </BRAND>
    <BRAND identifier="CP1809_E2">
      <TEXTOVERVIEW>730_64-BIT</TEXTOVERVIEW>
    </BRAND>
    <BRAND identifier="B1809_E3">
      <TEXT>Empty</TEXT>
    </BRAND>
    <BRAND identifier="B1809_E4">
      <TEXT>Empty</TEXT>
    </BRAND>
  </BRANDS>
</PARTS>

I need to make modifications such as:

<BRANDS>
  <BRAND identifier = "CP1809_B1">
    <TEXTCONTENT>Geforce_GT<TEXTCONTENT>
  </BRAND>
  <BRAND identifier = "CP1809_B2">
    <TEXTCONTENT>730_64-BIT<TEXTCONTENT>
  </BRAND>
  <!-- ... -->
<BRANDS>
1
  • 1
    Use DOM parser and make the changes and write it back. Commented Mar 21, 2016 at 13:00

1 Answer 1

1

XSLT can do that (see example at http://xsltransform.net/ej9EGda):

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:key name="desc" match="PART/BRAND" use="."/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="BRANDS/BRAND[key('desc', TEXTCONTENT)]/@identifier">
        <xsl:attribute name="{name()}">
            <xsl:value-of select="key('desc', ../TEXTCONTENT)/@id"/>
        </xsl:attribute>
    </xsl:template>

</xsl:transform>

On the Java platform you have a choice of XSLT processors like Saxon 9 for XSLT 2.0 or Xalan or Saxon 6 for XSLT 1.0 and the built-in version of Xalan in the JRE.

Sign up to request clarification or add additional context in comments.

2 Comments

Will you please give me a solution using Java? please.
Well, my suggestion is to run above XSLT with Java, the tutorial docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html has an example in Stylizer.java on how to run XSLT in Java. See also docs.oracle.com/javase/7/docs/api/javax/xml/transform/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.