search and replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • foolproofplan@gmail.com

    #1

    search and replace

    Hey everyone. I am running into a problem with unique ids that need to
    be compared in two xml files. The actual object name is represented
    with its unique id later in the xml file, so i need to do a search on
    an id, find its name, and then replace the original id with the name
    instead. If this doesn't make much sense, here is an example:

    <DBViewer template_id=" .... />
    <Fruits type="ObjectLin k" >:1170775272:45 9887139:</Fruits>
    </DBViewer>

    <excel_databa se template_id=":3 24:431:" id=":1170775272 :459887139:"
    name="banana" />

    In this example, the Fruit ObjectLink should be "banana" because it
    has the same id as the database below.

    I have already been doing xsl transformations on this file, so if that
    is possible for this situation, that would be great. How do you guys
    reccommend going about this?

  • roy axenov

    #2
    Re: search and replace

    On Feb 6, 5:57 pm, foolproofp...@g mail.com wrote:
    Hey everyone. I am running into a problem with unique ids
    that need to be compared in two xml files. The actual
    object name is represented with its unique id later in
    the xml file, so i need to do a search on an id, find its
    name, and then replace the original id with the name
    instead. If this doesn't make much sense, here is an
    example:
    >
    <DBViewer template_id=" .... />
    Ouch. That's not particularly well-formed.
    <Fruits
    type="ObjectLin k" >:1170775272:45 9887139:</Fruits>
    </DBViewer>
    >
    <excel_databa se template_id=":3 24:431:"
    id=":1170775272 :459887139:" name="banana" />
    >
    In this example, the Fruit ObjectLink should be "banana"
    because it has the same id as the database below.
    You should've posted some code. What precisely your problem
    is? Something like this:

    <xsl:template match="*[@type='ObjectLi nk']">
    <xsl:copy>
    <xsl:apply-templates
    select=
    "
    //excel_database[@id=current()/text()]/@name
    "/>
    </xsl:copy>
    </xsl:template>

    ....should do the trick.

    --
    roy axenov

    Comment

    • Joseph Kesselman

      #3
      Re: search and replace

      If it really is an XML ID (declared as such in the DTD or schema), you
      may be able to speed that up by replacing
      //excel_database[@id=current()/text()]/@name
      with
      id(current())/@name
      (Take the value of the current node, do a table lookup to find the
      element which carries that id value, take its name attribute.) The
      advantage is that this avoids having to search the whole document for
      the desired entry; the disadvantage is that unless it's properly
      declared it doesn't work.

      If you have to use the search approach, you may want to consider
      modifying it:
      //excel_database[@id=current()][1]/@name
      .... assuming that the ID is unique, this generates the same result but
      avoids wasting time searching for another instance.

      BTW, testing current()/text() checks whether a single text=-node child
      has that value -- which may or may not be what you want if there's
      markup inside the current() node. If you want the string value of the
      whole node, you can just test it directly or use the string() function
      (which comes out to the same thing).

      --
      Joe Kesselman / Beware the fury of a patient man. -- John Dryden

      Comment

      • foolproofplan@gmail.com

        #4
        Re: search and replace

        I had tried using your code:

        <xsl:template match="*[@type='ObjectLi nk']">
        <xsl:copy>
        <xsl:apply-templates
        select=
        "
        //excel_database[@id=current()/text()]/@name
        "/>
        </xsl:copy>
        </xsl:template>

        which works perfectly for the certain xml file I was talking about.

        I also tried using it for another one:

        <?xml version="1.0" encoding="UTF-8"?>
        <EnCapta>
        <Document type="Part" id=":1170780883 :1282681815:" name="New
        Document" >
        <FileName>\Ne w Document</FileName>
        <Unit/>
        <ApplicationDat a id=":1170780887 :48766733:" name="Hierarchy App" >
        <ApplicationRef erence id_ref=":213390 45:1265399:" >
        <Name>Hierarchy App</Name>
        <MajorVersion>0 </MajorVersion>
        <MinorVersion>0 </MinorVersion>
        </ApplicationRefe rence>
        <Colors template_id=":2 34334:9087980:" id=":1170780887 :2185726:"
        name="Colors Galore!" >
        <Name type="FixedStri ng" >Colors Galore!</Name>
        <Index type="Real" >0</Index>
        </Colors>
        <Blue template_id=":0 :534:" id=":1170780890 :1627341938:" name="I'm
        Blue" >
        <Name type="FixedStri ng" >I'm Blue</Name>
        <Index type="Real" >0</Index>
        <Parent type="ObjectLin k" >:1170780887:21 85726:</Parent>
        </Blue>
        <Green template_id=":1 2045115:5403534 :" id=":
        1170780894:1308 108165:" name="I'm Green" >
        <Name type="FixedStri ng" >I'm Green</Name>
        <Index type="Real" >0</Index>
        <Parent type="ObjectLin k" >:1170780887:21 85726:</Parent>
        </Green>
        <Red template_id=":1 20045115:534:" id=":1170780900 :313735256:"
        name="I'm Red" >
        <Name type="FixedStri ng" >I'm Red</Name>
        <Index type="Real" >0</Index>
        <Parent type="ObjectLin k" >:1170780887:21 85726:</Parent>
        </Red>
        <Link_Hierarc hy template_id=":1 20888115:534:" id=":
        1170780904:1346 796842:" name="Link_Hier archy" >
        <Root type="ObjectLin k" >:1170780900:31 3735256:</Root>
        </Link_Hierarchy>
        <Portal_Hierarc hy template_id=":1 77778115:534:" id=":
        1170780908:1320 06292:" name="Portal_Hi erarchy" >
        <Root type="ObjectLin k" >:1170780887:21 85726:</Root>
        </Portal_Hierarch y>
        </ApplicationData >
        </Document>
        </EnCapta>

        and reworked the code as this:

        <xsl:template match="*[@type='ObjectLi nk']">
        <xsl:copy>
        <xsl:apply-templates
        select="//*[@id=current()/text()]/@name"/>
        </xsl:copy>
        </xsl:template>

        However, nothing is copied into the ObjectLinks.

        Am I wrong on using * as a wildcard?

        Comment

        • Joe Kesselman

          #5
          Re: search and replace

          One immediate reaction: You wrote
          select="//*[@id=current()/text()]/@name"/>
          "Find any element in the document whose id attribute's value equals ONE
          OF THE TEXT CHILDREN of the current node, and process its name attribute."

          I suspect you meant

          "Find any element whose id attribute's value equals THE CONTAINED TEXT
          VALUE of the current node, and process its name attribute."

          which would be written as
          select="//*[@id=current()]/@name"/>

          (I haven't looked deeply enough to see whether this is actually what's
          causing your problem, but it's an important distinction to learn.)


          --
          () ASCII Ribbon Campaign | Joe Kesselman
          /\ Stamp out HTML e-mail! | System architexture and kinetic poetry

          Comment

          • Andrew

            #6
            RE: search and replace

            Hey everyone. I am running into a problem with unique ids that need to
            be compared in two xml files. The actual object name is represented
            with its unique id later in the xml file, so i need to do a search on
            an id, find its name, and then replace the original id with the name
            instead. If this doesn't make much sense, here is an example:
            >
            <DBViewer template_id=" .... />
            <Fruits type="ObjectLin k" >:1170775272:45 9887139:</Fruits>
            </DBViewer>
            >
            <excel_databa se template_id=":3 24:431:" id=":1170775272 :459887139:"
            name="banana" />
            >
            In this example, the Fruit ObjectLink should be "banana" because it
            has the same id as the database below.
            >
            I have already been doing xsl transformations on this file, so if that
            is possible for this situation, that would be great. How do you guys
            reccommend going about this?
            Don't testing it for xml , but possible it can help
            -----------
            You can use RQ Search and Replace to search and replace HTML code for tags, attribute names, or values.
            For example you can change the font size of one font type without affecting any others.
            [font size="2" face="Arial"] will be changed to [font size="3" face="Arial"], but tag [font size="2" face="Verdana"] remain the same size="2".
            You can delete or change HTML tags, it's attributes or attributes values.
            Don't worry about attribute's order in tag - program parsed HTML code and get all attributes and its values properly.
            -------------------
            homepage - http://www.miraxem.com/rqsr.html

            WBR, Andrew




            BizTalk Utilities - Frustration free BizTalk Adapters
            http://www.topxml.com/biztalkutilities

            Comment

            Working...