karthic
karthic

Reputation: 175

xpath query xml

I have an input xml as below

<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>

The output here i want the same output like below

<maindocument>
<import>
<doctype>Valuable</doctype>
<docint>text</docint>
</import>
</maindocument

I am trying for the above code in xslt With the help of absolute path and relative path my question is how i will get into testing element then i have to select import element from there.

Please guide me

Regards Karthic

Upvotes: 2

Views: 99

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

With the help of absolute path and relative path my question is how i will get into testing element then i have to select import element from there.

Actually, there is no need to specify a complete path to testing and check that it has an import child.

The XSLT processing model (built-in templates), combined with your templates and proper template match patterns, does the navigation for you.

The built-in templates are selected for execution if there is no explicit template that matches a particular node. he cumulative result of applying the built-in templates is that the document tree is traversed and only text nodes are output.

One can specify templates to override the built-in ones only for nodes that should be processed in a different, specific way. When the built-in templates apply templates for a child of a node and there is explicit (user-provided) template matching that node, this explicit template is selected for execution/processing of that node.

This short XSLT 1.0 transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <maindocument>
        <xsl:apply-templates/>
     </maindocument>
 </xsl:template>

 <xsl:template match="import/downloading">
  <import>
    <doctype><xsl:value-of select="substring-before(., ' ')"/></doctype>
    <docint><xsl:value-of select="substring-after(., ' ')"/></docint>
  </import>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided XMLdocument:

<maindocument>
    <first>
        <testing>random text</testing>
        <checking>random test</checking>
    </first>
    <testing>
        <testing>sample</testing>
        <checking>welcome</checking>
        <import>
            <downloading>valuable text</downloading>
        </import>
    </testing>
</maindocument>

produces the wanted, correct result:

<maindocument>
   <import>
      <doctype>valuable</doctype>
      <docint>text</docint>
   </import>
</maindocument>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167401

Well copying the right elements to the output is easy, and with XSLT 2.0 it is also easy to break up the contents of the downloading element:

<xsl:output indent="yes"/>

<xsl:template match="maindocument">
  <xsl:copy>
    <xsl:apply-templates select="testing/import"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="import">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="import/downloading">
  <xsl:analyze-string select="." regex="(\w)(\w+)\s+(\w+)">
    <xsl:matching-substring>
      <doctype>
        <xsl:value-of select="concat(upper-case(regex-group(1)), regex-group(2))"/>
      </doctype>
      <docint>
        <xsl:value-of select="regex-group(3)"/>
      </docint>
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

Using Saxon 9.4 the above stylesheet transforms the input

<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>

into

<maindocument>
   <import>
      <doctype>Valuable</doctype>
      <docint>text</docint>
   </import>
</maindocument>

The use of analyze-string is meant as an example, it is not clear to me from your sample what kind of input the downloading element can have and how you want to handle that. So post more details if the above is not sufficient.

Upvotes: 2

Related Questions