1

I have two xml files 1. book.xml and 2. Store.xml , I want to read Uid from book.xml and update value in attribute BookId in store.xml

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

<Book Type="0001" Bkey="book1" BName="abc">
    <Pages >
        <Page LineNo="1.1" >
            <Lines Size="1.00"
                Total="0.00" Lock="N"
               Amount="20.00" Unit="20.00"/>
            <Chars>
                <Char
                    Uid="123456"
                    NoOfChars="1000" Processed="N"
                    Knowledge="Y">
                    
                    </Char>
                                     
                
            </Chars>
            
        </Page>
    </Pages>
  
</Book>`

Store.xml

   <bookstore>
   <book BookId="">
        <Bkey> book1 </Bkey>
       <title lang="en">Everyday Italian</title>
       <author>Giada De Laurentiis</author>
       <year>2005</year>
       <price>30.00</price>
       </book>
 
</bookstore>

I need xslt to read Uid value from Book.xml and update BookId= attribute in Store.xml

looking for below xml:

<bookstore>
   <book BookId="123456">
        <Bkey> book1 </Bkey>
       <title lang="en">Everyday Italian</title>
       <author>Giada De Laurentiis</author>
       <year>2005</year>
       <price>30.00</price>
       </book>
 
</bookstore>

1
  • 1
    "I need xslt to ..." - Not how this site works. You post your own code, explain what issues you have with it, and people will help fix your code. You don't come here and tell people what you need. Commented Nov 20, 2020 at 12:23

1 Answer 1

1

Below is the XSLT which updates the Book Id in the Store.XML. You need to pass the Book.XML as the parameter for the XSLT processing.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:param name="book_docu" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="book/@BookId">
    <xsl:variable name="bookKeyFromStore">
      <xsl:value-of select="parent::node()/Bkey" />
    </xsl:variable>
    <xsl:attribute name="BookId">
      <xsl:value-of select="$book_docu/Book[@Bkey=$bookKeyFromStore]/Pages/Page/Chars/Char/@Uid" />
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

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.