"value substitution/population" in template XML using XPath

Discussion in 'XML' started by Bilal, Sep 15, 2006.

  1. Bilal

    Bilal Guest

    Hello
    I'm stuck on this problem for quite some time and hope somebody woul
    be able to guide me

    Basically, I need to populate a large number of "template" XML file
    which have all elements/attributes etc. defined but the values in thes
    elements/attributes might be blank or place holders, as two example
    below

    Example - File type
    <?xml version="1.0" encoding="UTF-8"?
    <fileType1
    <element1
    <element2 attr2="xxx"
    <element3
    <element4 attr4="yyy"
    </element4
    </element3
    </element2
    </element1
    </fileType1

    o

    Example - File type
    <?xml version="1.0" encoding="UTF-8"?
    <fileType2
    <element1
    <element2 attr2="xxx"
    <element13
    <element14
    <element15>ZZZ</element15
    </element14
    </element13
    </element2
    <element3
    </element3
    </element1
    </fileType2

    and the new values to be populated (or atleast their XPath) ar
    indicated in a seperate XML data file as, and easily obtained a
    xpath/value pair using Java or XSLT etc.

    <?xml version="1.0" encoding="UTF-8"?
    <newValues
    <file type="fileType2"
    <element
    <x-path>/fileType2/element1/element3</x-path
    <value>newValue3</value
    </element
    <element
    <x-path>/fileType2/element1/element2/@attr2</x-path
    <value>ABC</value
    </element
    <element
    <x-path>/fileType2/element1/element2/element13/element14/element15</x-path
    <value>newValue15</value
    </element
    </file
    <file type="fileType1"
    <element
    <x-path>/fileType1/element1/element2/@attr2</x-path
    <value>123</value
    </element
    <element
    <x-path>/fileType1/element1/element2/element3/element4/@attr4</x-path
    <value>999</value
    </element
    </file
    </newValues


    So what is the best approach to read the 'template' example file(s) an
    output it with new values for xpath indicated in the data file

    I have used DOM/JDOM to perform this task at a limited scale, but th
    complicating factors are

    - the number of unique "template" files is rather large (al
    generated from relevent XSD
    - the structure of each template is quite deep/comple
    - the number of xpath/value would be arbitrary (say min of 5 "fields
    to be changed, max all of them!

    Essentially this task is a 'value substitution' or 'data population
    problem for an XML file, where the xpath/value pair are indicated i
    another XML file. IMHO (re)constructing the output XML from scratc
    appears would be overkill, and not feasbile to do it programatically i
    Java code etc. Hence my search for a more efficient way to do it, sa
    with XSLT or some other technique, which 'processes' the original XM
    input file WITHOUT altering the structure but substituting the desire
    values as required.

    Would appreciate any and all help in this regards

    Regards

    Bilal B

    --
    Bila
    -----------------------------------------------------------------------
    Bilal's Profile: http://techiegroups.com/member.php?userid=33
    View this thread: http://www.techiegroups.com/showthread.php?t=11662
     
    Bilal, Sep 15, 2006
    #1
    1. Advertising

  2. Y'know, I'd consider (ab)using XSLT for this.

    Start with the standard identity transform, to handle most of the document.

    Then as the equivalent of your

    > <element>
    > <x-path>/fileType2/element1/element3</x-path>
    > <value>newValue3</value>
    > </element>


    write a template something like:

    <xsl:template match="/fileType2/element1/element3">
    <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:choose>
    <xsl:when test="node()"> <!-- If not empty -->
    <xsl:apply-templates select="node()"/>
    <xsl:when>
    <xsl:eek:therwise>newValue3</xsl:eek:therwise>
    </xsl:choose>
    </xsl:copy>
    </xsl:template>

    Obviously this could handle more complicated conditions/operations as well.

    Note that if you don't want to write this code by hand it would be
    straightforward to write a stylesheet which generates this stylesheet
    from the <newValues> sketch you showed us.

    XPath-match-and-transform is exactly what XSLT is designed for. This
    isn't a typical application for it, but it's can do the job, and
    probably beats hand-coding a new application for the purpose.



    Having said all that: You said you're using schemas. I believe schemas
    can specify default values. Is there a reason you aren't using that
    mechanism rather than inventing a new one?





    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Sep 16, 2006
    #2
    1. Advertising

  3. Bilal

    Bilal Guest

    Hi Joe,
    Thanks for your reply; I'll explore your suggestions further to fully
    understand them as my XSLT experience is, to be honest, between advanced
    beginner & intermediate :)

    >Note that if you don't want to write this code by hand it
    >would be straightforward to write a stylesheet which
    >generates this stylesheet from the <newValues> sketch you
    >showed us.


    As the <newValues> sketch/file isn't static, I think that would be the
    only viable way to do. Hmm... need bit more reading up on that!

    >Having said all that: You said you're using schemas. I
    > believe schemas can specify default values. Is there a
    > reason you aren't using that mechanism rather than
    > inventing a new one?


    Yes, I do have the schema so if the task can be done by using the
    schema, I would try that approach as well. Indeed default values for
    elements/attributes can be specified in the schema as below:

    <xs:element name="color" type="xs:string" default="red"/>

    The values for elements/attributes as exampled in the <newValues>
    sketch will differ from case to case, so unsure how that would be
    useful? Wouldn't that necessitate changing the schema file to generate a
    new instance xml? Also, how would one use the xpath to navigate the
    schama inself? Pardon my ignorance if that is a something obvious but my
    XSLT experience has been limited to plain XML files rather than XSD
    files, though I recognize that the principle remains the same for the
    two. So could you elaborate please?

    Thanks again!

    Regards,

    Bilal B.

    *** Sent via Developersdex http://www.developersdex.com ***
     
    Bilal, Sep 18, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,994
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,444
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    541
  4. goog
    Replies:
    0
    Views:
    518
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,662
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page