Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

How to apply two templates to the same set of nodes, specifically all the nodes in a XML

I trying to create an output XML file from an input XML file. Some of the nodes need to be copied from input to output, some with be omitted, and a few of the nodes will have their text modified in some way. However in addition to this, I need to trim the whitespace from the text of every node. I assume that the best way to do this is to have two templates called on the same set of nodes using a mode or some other attribute, but I cannot figure out how to do this. There are too many nodes to manually apply a trim() to every node.

The code to remove the whitespace works by itself (solution from another Stackoverflow), but I do not know how to modify the XML using another template and then apply these two templates. I expect the solution to be something like:

<xsl:template match="/">

do transformation work...

jump to match="node()" template

</xsl:template>

Remove whitespace solution:

<xsl:template match="node()">
    <xsl:copy>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

<xsl:template match="text()">
    <xsl:value-of select="normalize-space(.)" />
</xsl:template>

Here is a sample input:

<ABC Id="64" Author="FirstName">
    <Note Type="Test" ID="01">
        <Note.1>string     </Note.1>
        <Note.2>
            <Point.1>string2        </Point.1>
            <Point.2>hello</Point.2>
        </Note.2>
    </Note>
</ABC>

Expected output:

<ABC Id="64" Author="FirstName">
   <Note Type="Test" ID="01">
      <Note.1>STRING</Note.1> 
      <Note.2>
         <Point.1>string2</Point.1>
      </Note.2>
    </Note>
</ABC>

Note.1 was copied from source, transformed to uppercase and had whitespace removed. Note.2/Point.1 had whitespace removed. Note.2/Point.2 was not copied from the source.

Edit --- My current code. It properly removes the whitespace. However the resulting XML contains every node from the source with the whitespace removed, instead of only those nodes copied in the first transformation. I am storing these nodes in a variable and then passing that variable into my remove whitespace template. Any thoughts on why the whitespace-removing template is acting on the original set instead of the set contained in the variable?

  <xsl:template match="/">
    <!--Store the results in a variable called transformation_result-->
    <xsl:variable name="transformation_result">
      <!--Go to the normal transformation template -->
      <xsl:call-template name="transformation"/>
    <!--Results now in $transformation_result -->
    </xsl:variable>    
    <!-- If you want to see what is in the variable -->
    <!--<xsl:copy-of select="$transformation_result"/>-->
    <!-- Go to the template that copies all input -->
    <xsl:call-template name="copy_for_whitespace">
      <!-- The $tranformation_result is the parameter passed in by the name of: input -->
      <xsl:with-param name="input" select="$transformation_result"/>
    </xsl:call-template>
  </xsl:template>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <xsl:template name="transformation">
    <imp1:HIJ>
      <xsl:copy-of select="/imp1:HIJ/imp1:ABC">
        <?oracle-xsl-mapper-position imp1:ABC?>
      </xsl:copy-of>
    </imp1:HIJ>
  </xsl:template>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <xsl:template name="copy_for_whitespace" match="node()">          
    <xsl:param name="input"/>
    <xsl:variable name="test">
    <xsl:copy>                                                 
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
        </xsl:variable>
    <xsl:copy-of select="$test"/>
  </xsl:template>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <xsl:template match="text()">                                     
    <xsl:value-of select="normalize-space()"/>
  </xsl:template>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is not really an answer, but it's too long for a comment.

If you apply two different templates (using modes) to the same node, you will end up with two resulting nodes. I don't think that's what you want, so you need to do one of the two:

(a) Do the transformation in two passes: apply the first set of templates to the original nodes in the source document, while channeling the result to a variable; then apply the second set of templates to (new) nodes in the variable, this time channeling the result to the output.

(b) Merge the two transformations into one: unfortunately, you did not show us the templates for the first transformation (which is why this cannot be an answer), but as an example, if you want to shift a node's text to uppercase and trim the whitespace, you can do simply:

<xsl:template match="Note.1">
    <xsl:copy>
        <xsl:value-of select="normalize-space(upper-case(.))"/>
    </xsl:copy>
</xsl:template>

If normalizing whitespace is all your second transformation is supposed to do, then I believe option (b) would be the better choice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...