find sibling node adjacent to each other

42 views Asked by At

I have a huge xml and xsl and I am getting some error while tranforming it. I want to find out that if the 2 child nodes are adjacent to each other inside a parent node, then call appropriate template.

Let's see the below fragment from a existing big xml. There can be many nodes between mydata:Headings and mydata:ActionText. Also, mydata:ActionText could have nested nodes inside them.

<mydata:Headings>
    <mydata:heading1>Heading one</mydata:heading1>
    <mydata:ActionText>
        <mydata:title>
           This is a test title<?start_mark?> show this value<?end-mark?> 
        </mydata:title>
    </mydata:ActionText>
    <?start_mark?> This appears in a different font and color inside another template<?end-mark?>
</mydata:Headings>

Now I need to find out inside a template that if mydata:ActionText and processing instruction start_mark exists adjacent to each other. If both exists, then I need to call template this

<xsl:apply-templates select="mydata:ActionText|processing-instruction()"/>

If only mydata:ActionText exists, then I need to call the template like this

<xsl:apply-templates select="mydata:ActionText"/>

How to check for this?

My template for is as shown below:

<xsl:template match="mydata:Headings"> Here I have few xsl codes. I want to check the above condition here. </xsl:template>

1

There are 1 answers

3
Michael Kay On

It's going to be easiest if you're stripping whitespace text nodes with <xsl:strip-space elements="*"/>. In that case you can write

myData:ActionText[following-sibling::node()[1]
                    [self::processing-instruction(start_mark)]]

If you have to account for a whitespace text node between the element and the processing instruction then it becomes

myData:ActionText
  [following-sibling::node()
    [not(self::text()[normalize-space(.)=''])]]
    [1]
    [self::processing-instruction(start_mark)]
  ]

I wonder though if you really need this? If you're trying to decide whether to do

<xsl:apply-templates select="mydata:ActionText|processing-instruction()"/>

or

<xsl:apply-templates select="mydata:ActionText"/> 

then why not just do the former? If there's no processing instruction then the effect is identical to the latter instruction.

Finally, a comment on your question. You start by saying

I have a huge xml and xsl and I am getting some error while tranforming it.

and presumably there is some reason you think we need to know that. But you don't say what the error is - is it related to the fact that the XML is huge? Do you have a performance problem, or a problem with the scaleability of the application, or is it a question about how to achieve the functionality? By throwing in these extra facts, you've made it confusing.