简体   繁体   中英

XSLT for reading attribute from one xml and replace in another xml

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM