嵌套的标签类未在xsl xml转换中应用。

huangapple go评论83阅读模式
英文:

Nested span tag class not applied in xsl xml transformation

问题

我想确保您想要的部分是正确的,以下是输出HTML中的一部分,其中第4行数据已被更正以显示为"book 4 .......",而不是"book 4 <span class="Dots"/>"。

&lt;tr&gt;
&lt;td&gt; book 4 .......&lt;/td&gt;
&lt;td&gt;1004&lt;/td&gt;
&lt;tr&gt;

请让我知道是否需要更多帮助。

英文:

I am new to xml and XSLT transformation. My data is in xml and is being transferred to html using XSLT. I am using java program to transform. I have a made a simple xml and XSL to show the problem I am facing. The xml has the data for books. Each book's title and location is being displayed inside a table's td. There is a <?del_mark> tag inside the xml. This tag means, that the content specified in the data attribute of this tag needs to be enclosed inside a strike out and a P tag.


&lt;?del-mark data=&quot;&lt;tr&gt;&lt;td&gt;book 5 &lt;span class=&quot;Dots&quot;/&gt;&lt;/td&gt;&lt;td&gt;1005&lt;/td&gt;&lt;/tr&gt;&quot; /&gt;

In the stylesheet there is a template to handle the processing instruction (del_mark)and it takes the data attribute's value and replaces it inside a p and strike through tag. It is working fine, but in the final html, it is not replacing the &lt;span class=&quot;Dots&quot;/&gt; with .... for the processing instruction template.

My problem is: There is a template for span tag which converts the span to dots using &lt;span class=&quot;Dots&quot;/&gt;. This template is not getting used and the tag appear as is <span class="Dots"/>instead of dots as described in the template.

I have a java program which reads the sample xml and checks for the processing tag <?del_mark> and reads the data part of it and converts all the &lt; and &quot; to < and ". Then, this intermediate xml is used to as input xml for the transformation. The intermediate xml is also given below.

Please let me know if you need more info.

SAMPLE XML

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;?xml-stylesheet type=&quot;text/xsl&quot;?&gt;
&lt;books&gt;
    &lt;book&gt;
        &lt;title&gt;Book 1&lt;/title&gt;
        &lt;location&gt;1000&lt;/location&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Book 2&lt;span class=&quot;Dots&quot;/&gt;
        &lt;/title&gt;
        &lt;location&gt;1002&lt;/location&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Book 3&lt;/title&gt;
        &lt;location&gt;1003&lt;/location&gt;
    &lt;/book&gt;
    &lt;?del_mark data=&amp;quot;&amp;lt;tr style=&amp;quot;keep-with-next.within-page:always;&amp;quot;&gt;
        &amp;lt;td width=&amp;quot;280&amp;quot;&gt;
            &amp;lt;p class=&amp;quot;HangingIndent&amp;quot;&gt;Book&amp;lt;span class=&amp;quot;Dots&amp;quot;/&gt;
            &amp;lt;/strike&gt;
        &amp;lt;/p&gt;
        &amp;lt;div style=&amp;quot;margin:0 0 1em 0;&amp;quot;&gt;&amp;lt;/div&gt;
    &amp;lt;/td&gt;
    &amp;lt;td align=&amp;quot;center&amp;quot; valign=&amp;quot;bottom&amp;quot; width=&amp;quot;136&amp;quot;&gt;1004&amp;lt;/td&gt;
&amp;lt;/tr&gt;&amp;quot;?&gt;
    &lt;book&gt;
        &lt;title&gt;Book 5&lt;/title&gt;
        &lt;location&gt;1005&lt;/location&gt;
    &lt;/book&gt;
&lt;/books&gt;

** SAMPLE XSL**

&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot;
    xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
    xmlns:fo=&quot;http://www.w3.org/1999/XSL/Format&quot;
    xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
    xmlns:fn=&quot;http://www.w3.org/2005/02/xpath-functions&quot;
    xmlns:xdt=&quot;http://www.w3.org/2005/02/xpath-datatypes&quot;
    xmlns:xhtml=&quot;http://www.w3.org/1999/xhtml&quot;
    xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;
    xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
    xmlns:math=&quot;java.lang.Math&quot;&gt;
    &lt;xsl:output method=&quot;html&quot; encoding=&quot;UTF-8&quot;/&gt;
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;html&gt;
            &lt;head&gt;
                &lt;title&gt;Books&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;p&gt; This a sample xml/xsl example&lt;span class=&quot;Dots&quot;/&gt;
                &lt;/p&gt;
                &lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;
                    &lt;THEAD&gt;
                        &lt;TR&gt;
                            &lt;TD width=&quot;35%&quot;&gt;
                                &lt;B&gt;Title&lt;/B&gt;
                            &lt;/TD&gt;
                            &lt;TD width=&quot;15%&quot;&gt;
                                &lt;B&gt;Location&lt;/B&gt;
                            &lt;/TD&gt;
                        &lt;/TR&gt;
                    &lt;/THEAD&gt;
                    &lt;TBODY&gt;
                        &lt;xsl:for-each select=&quot;books/book|books/processing-instruction()&quot;&gt;
                            &lt;tr&gt;
                                &lt;xsl:apply-templates select=&quot;.&quot;/&gt;
                            &lt;/tr&gt;
                        &lt;/xsl:for-each&gt;
                    &lt;/TBODY&gt;
                &lt;/table&gt;
            &lt;/body&gt;
        &lt;/html&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match=&quot;title&quot;&gt;
        &lt;td&gt;
            &lt;xsl:value-of select=&quot;.&quot; /&gt;
        &lt;/td&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match=&quot;location&quot;&gt;
        &lt;td&gt;
            &lt;xsl:value-of select=&quot;.&quot; /&gt;
        &lt;/td&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match=&quot;xhtml:span | span&quot;&gt;

        &lt;xsl:if test=&quot;@class=&#39;Spaces&#39;&quot;&gt;
      _________
        &lt;/xsl:if&gt;
        &lt;xsl:if test=&quot;@class=&#39;Dots&#39;&quot;&gt;
      ..........
        &lt;/xsl:if&gt;

        &lt;xsl:apply-templates/&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match=&quot;processing-instruction(&#39;del_mark&#39;)&quot;&gt;
        &lt;xsl:text disable-output-escaping=&quot;yes&quot;&gt;&lt;![CDATA[&lt;font color=&quot;yellow&quot;&gt;&lt;strike&gt;]]&gt;&lt;/xsl:text&gt;
        &lt;xsl:variable name=&quot;dval&quot; select=&quot;substring-after(current(), &#39;data=&#39;)&quot;/&gt;
        &lt;xsl:variable name=&quot;v1&quot;&gt;
            &lt;xsl:value-of select=&quot;$dval&quot; disable-output-escaping=&quot;yes&quot;/&gt;
        &lt;/xsl:variable&gt;
        &lt;xsl:value-of select=&quot;substring($v1, 2, string-length($v1) - 2)&quot; disable-output-escaping=&quot;yes&quot;/&gt;
        &lt;xsl:text disable-output-escaping=&quot;yes&quot;&gt;&lt;![CDATA[&lt;/strike&gt;&lt;/font&gt;]]&gt;&lt;/xsl:text&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

** INTERMEDIATE XML **

&lt;?xml-stylesheet type=&quot;text/xsl&quot;?&gt;&lt;books&gt;
    &lt;book&gt;
        &lt;title&gt;Book 1&lt;/title&gt;
        &lt;location&gt;1000&lt;/location&gt;

    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Book 2&lt;span class=&quot;Dots&quot;/&gt;
        &lt;/title&gt;
        &lt;location&gt;1002&lt;/location&gt;

    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Book 3&lt;/title&gt;
        &lt;location&gt;1003&lt;/location&gt;

    &lt;/book&gt;
    &lt;?del_mark &lt;p style=&quot;display:inline;&quot;&gt;data=&quot;&lt;tr style=&quot;keep-with-next.within-page:always;&quot;&gt;
        &lt;td width=&quot;280&quot;&gt;
            &lt;p class=&quot;HangingIndent&quot;&gt;Book&lt;span class=&quot;Dots&quot;/&gt;
            &lt;/strike&gt;
        &lt;/strike&gt;&lt;/p&gt;&lt;div style=&quot;margin:0 0 1em 0;&quot;&gt;&lt;/div&gt;
        &lt;div style=&quot;margin:0 0 1em 0;&quot;&gt;&lt;/div&gt;
    &lt;/td&gt;
    &lt;td align=&quot;center&quot; valign=&quot;bottom&quot; width=&quot;136&quot;&gt;1004&lt;/td&gt;
&lt;/tr&gt;&quot;?&gt;

    &lt;book&gt;
        &lt;title&gt;Book 5&lt;/title&gt;
        &lt;location&gt;1005&lt;/location&gt;

    &lt;/book&gt;
&lt;/books&gt;

HTML Output

&lt;html xmlns:math=&quot;java.lang.Math&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:fo=&quot;http://www.w3.org/1999/XSL/Format&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:fn=&quot;http://www.w3.org/2005/02/xpath-functions&quot; xmlns:xdt=&quot;http://www.w3.org/2005/02/xpath-datatypes&quot; xmlns:xhtml=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
   &lt;head&gt;
      &lt;title&gt;Books&lt;/title&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;p&gt; This a sample xml/xsl example&lt;span class=&quot;Dots&quot;&gt;&lt;/span&gt;&lt;/p&gt;
      &lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;
         &lt;THEAD&gt;
            &lt;TR&gt;
               &lt;TD width=&quot;35%&quot;&gt;&lt;B&gt;Title&lt;/B&gt;&lt;/TD&gt;
               &lt;TD width=&quot;15%&quot;&gt;&lt;B&gt;Location&lt;/B&gt;&lt;/TD&gt;
            &lt;/TR&gt;
         &lt;/THEAD&gt;
         &lt;TBODY&gt;
            &lt;tr&gt;
               &lt;td&gt;Book 1&lt;/td&gt;
               &lt;td&gt;1000&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
               &lt;td&gt;Book 2
               &lt;/td&gt;
               &lt;td&gt;1002&lt;/td&gt;               
            &lt;/tr&gt;
            &lt;tr&gt;
               &lt;td&gt;Book 3&lt;/td&gt;
               &lt;td&gt;1003&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;&lt;font color=&quot;yellow&quot;&gt;&lt;strike&gt;&lt;tr style=&quot;keep-with-next.within-page:always;&quot;&gt;
               &lt;td width=&quot;280&quot;&gt;
               &lt;p class=&quot;HangingIndent&quot;&gt;Book&lt;span class=&quot;Dots&quot;/&gt;
               &lt;/strike&gt;
               &lt;/strike&gt;&lt;/p&gt;&lt;div style=&quot;margin:0 0 1em 0;&quot;&gt;&lt;/div&gt;
               &lt;div style=&quot;margin:0 0 1em 0;&quot;&gt;&lt;/div&gt;
               &lt;/td&gt;
               &lt;td align=&quot;center&quot; valign=&quot;bottom&quot; width=&quot;136&quot;&gt;1004&lt;/td&gt;
               &lt;/tr&gt;&lt;/strike&gt;&lt;/font&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
               &lt;td&gt;Book 5&lt;/td&gt;
               &lt;td&gt;1005&lt;/td&gt;
            &lt;/tr&gt;
         &lt;/TBODY&gt;
      &lt;/table&gt;
   &lt;/body&gt;
&lt;/html&gt;

IN the output html, I want the row 4 data to be shown as book 4... instead of book 4 &lt;span class=&quot;Dots&quot;/&gt;.

&lt;tr&gt;
&lt;td&gt; book 4 .......&lt;/td&gt;
&lt;td&gt;1004&lt;/td&gt;
&lt;tr&gt;

Thanks.

答案1

得分: 1

span节点的模板首先没有被应用,因为你没有尝试使用 xsl:apply-templates 应用它,其次因为你的文档实际上没有 span 节点。你的文档中只有一些数据,如果它被解析为XML,那么它们才会成为 span 节点。

在现代版本的XSLT中,有一个 parse-xml() 函数,允许你将处理指令中的词法XML转换为节点树,然后可以像通常一样处理它们(例如使用 xsl:apply-templates)。实现较早版本XSLT的处理器可能会有一个执行相同操作的扩展函数,或者它们可能允许你自己编写扩展函数(例如调用Java)。

在Saxon中还有一个扩展函数 get-pseudo-attributes(),允许你提取处理指令的 "data" 伪属性。

所以:

  1. 提取处理指令数据的内容,就像你现在所做的那样:

    <xsl:variable name="dval" select="substring-after(current(), 'data=')"/>

并去掉前导和尾随引号。

  1. 或者使用 saxon:get-pseudo-attribute('data')

  2. 对结果调用 parse-xml()

  3. 对结果调用 apply-templates

所以它可以简单地是这样的:

<xsl:template match="processing-instruction('del_mark')">
  <font color="yellow">
    <strike>
      <xsl:apply-templates 
         select="parse-xml(saxon:get-pseudo-attribute('data'))"
         xmlns:saxon="http://saxon.sf.net/"/>
    </strike>
  </font>
</xsl:template>

为了增加一些额外的模块化,我会将 span 的模板规则放在一个单独的模式中:

<xsl:mode name="nested-xml" on-no-match="shallow-copy"/>
<xsl:template match="span" mode="nested-xml">
 ...
</xsl:template>

然后:

<xsl:apply-templates 
             select="parse-xml(saxon:get-pseudo-attribute('data'))"
             mode="nested-xml"
             xmlns:saxon="http://saxon.sf.net/"/>
英文:

The template for the span node isn't being applied firstly because you make no attempt to apply it using xsl:apply-templates, and secondly because there is in fact no span node in your document. There's only some data that would be turned into a span node if it were parsed as XML.

In modern versions of XSLT there is a parse-xml() function that allows you to turn the lexical XML in your processing instruction into a tree of nodes, which can then be processed in the normal way (for example using xsl:apply-templates). Processors implementing earlier versions of XSLT may have an extension function that does the same thing, or they may allow you to write your own (for example by calling out to Java).

In Saxon there is also an extension function get-pseudo-attributes() that allows you to extract the "data" pseudo-attribute of your PI.

So:

  1. Extract the contents of the PI data as you do now:

    <xsl:variable name="dval" select="substring-after(current(), 'data=')"/>

and strip off the leading and trailing quotes.

  1. Alternatively, use saxon:get-pseudo-attribute(&#39;data&#39;)

  2. Call parse-xml() on the result

  3. Call `apply-templates on the result.

So it could simply be:

&lt;xsl:template match=&quot;processing-instruction(&#39;del_mark&#39;)&quot;&gt;
  &lt;font color=&quot;yellow&quot;&gt;
    &lt;strike&gt;
      &lt;xsl:apply-templates 
         select=&quot;parse-xml(saxon:get-pseudo-attribute(&#39;data&#39;)&quot;
         xmlns:saxon=&quot;http://saxon.sf.net/&quot;/&gt;
    &lt;/strike&gt;
  &lt;/font&gt;
&lt;/xsl:template&gt;

For a bit of extra modularity, I would put the template rule for span in a separate mode:

&lt;xsl:mode name=&quot;nested-xml&quot; on-no-match=&quot;shallow-copy&quot;/&gt;
&lt;xsl:template match=&quot;span&quot; mode=&quot;nested-xml&quot;&gt;
 ...
&lt;/xsl:template&gt;

and then

&lt;xsl:apply-templates 
             select=&quot;parse-xml(saxon:get-pseudo-attribute(&#39;data&#39;)&quot;
             mode=&quot;nested-xml&quot;
             xmlns:saxon=&quot;http://saxon.sf.net/&quot;/&gt;

huangapple
  • 本文由 发表于 2023年8月4日 06:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定