选择其子节点的 xsl:variable 值

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

select xsl:variable values of its child-nodes

问题

以下是代码部分的中文翻译:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
  xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
  xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
  xmlns:exsl="http://exslt.org/common"
  exclude-result-prefixes="table text office exsl"
>

  <xsl:output
    method="xml"
    indent="yes"
    encoding="UTF-8"
    omit-xml-declaration="no"
  />

  <xsl:template match="/">

    <xsl:variable name="columnHeadings-temp" xmlns="">
      <xsl:for-each select="//table:table/table:table-row[not(preceding::table:table-row)]//table:table-cell">
        <xsl:element name="heading">
          <xsl:attribute name="name">
            <xsl value-of select="normalize-space(text:p)" />
          </xsl:attribute>
          <xsl:value-of select="position()" />
        </xsl:element>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="columnHeadings" select="exsl:node-set($columnHeadings-temp)" />

    <html>
      <body>
        <table>
          <xsl:for-each select="//table:table/table:table-row">

            <xsl:if test="position() > 1">

              <tr>
                <td>
                  First Column Value
                  <xsl:value-of select="table:table-cell[number($columnHeadings/heading[@name='First_Name'])]/text:p" />
                </td>
                <td>
                  Second Column Value
                  <xsl:value-of select="table:table-cell[number($columnHeadings/heading[@name='Last_Name'])]/text:p" />
                </td>
              </tr>

            </xsl:if>

          </xsl:for-each>
        </table>
      </body>
    </html>

  </xsl:template>

</xsl:stylesheet>

希望这对你有所帮助。如果你有任何其他问题或需要进一步的帮助,请随时提出。

英文:

I have an .ods file and want to access the values of table-cells in table-rows by the value of the first column for the given row. So their heading in my case.

So the calc table looks like this:

First_Name | Last_Name
Peter      | Parker
Emma       | Stone
...

Here is my xslt-export-filter file:

SuperBasicExportFilter.xslt

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet
version=&quot;1.0&quot;
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:table=&quot;urn:oasis:names:tc:opendocument:xmlns:table:1.0&quot;
xmlns:text=&quot;urn:oasis:names:tc:opendocument:xmlns:text:1.0&quot;
xmlns:office=&quot;urn:oasis:names:tc:opendocument:xmlns:office:1.0&quot;
exclude-result-prefixes=&quot;table text office&quot;
&gt;
&lt;xsl:output
method=&quot;xml&quot;
indent=&quot;yes&quot;
encoding=&quot;UTF-8&quot;
omit-xml-declaration=&quot;no&quot;
/&gt;
&lt;xsl:template match=&quot;/&quot;&gt;
&lt;xsl:variable name=&quot;columnHeadings&quot;&gt;
&lt;xsl:for-each select=&quot;//table:table/table:table-row[not(preceding::table:table-row)]//table:table-cell&quot;&gt;
&lt;xsl:element name=&quot;heading&quot;&gt;
&lt;xsl:attribute name=&quot;name&quot; select=&quot;text:p&quot; /&gt;
&lt;xsl:value-of select=&quot;position()&quot; /&gt;
&lt;/xsl:element&gt;
&lt;/xsl:for-each&gt;
&lt;/xsl:variable&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h1&gt;Hello&lt;/h1&gt;
&lt;xsl:message&gt;columnHeadings: &lt;xsl:value-of select=&quot;$columnHeadings&quot; /&gt;&lt;/xsl:message&gt;
&lt;table&gt;
&lt;xsl:for-each select=&quot;//table:table/table:table-row&quot;&gt;
&lt;xsl:if test=&quot;position() &gt; 1&quot;&gt;
&lt;tr&gt;
&lt;td&gt;
First Column Value
&lt;xsl:value-of select=&quot;table:table-cell[1]/text:p&quot; /&gt;
&lt;!-- &lt;xsl:value-of select=&quot;table:table-cell[$columnHeadings/heading[@name=&#39;First_Name&#39;]]/text:p&quot; /&gt; --&gt;
&lt;/td&gt;
&lt;td&gt;
Second Column Value
&lt;xsl:value-of select=&quot;table:table-cell[2]/text:p&quot; /&gt;
&lt;!-- &lt;xsl:value-of select=&quot;table:table-cell[$columnHeadings/heading[@name=&#39;Last_Name&#39;]]/text:p&quot; /&gt; --&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/xsl:if&gt;
&lt;/xsl:for-each&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

The message shows "columnHeadings: 1234567891011121314" and so on. So it is setting the position values correctly.

I tried getting the values based on the "name" attribute on the "heading" element.
But I can't get the values individually in any way. It seems I can't use the $columnHeadings with any XPath expression. It just returns "Xpath evaluation returned no result".

I tried

  • wrapping the "heading" elements with a "columnHeadings" element inside the variable and setting the "as" value of the variable to "element()"
  • using the "node-set" function (after importing the "exslt" ns)
  • using &lt;xsl:variable name=&quot;columnHeadingsNode&quot; select=&quot;document(&#39;&#39;)//xsl:variable[@name = &#39;columnHeadings&#39;]&quot; /&gt; to then get the value
  • using the xsl:key element like &lt;xsl:key name=&quot;columnHeadings&quot; match=&quot;//table:table/table:table-row[not(preceding::table:table-row)]//table:table-cell&quot; use=&quot;text:p&quot; /&gt;
    - but this way I can't access it based on the "name"

What other things can I try to access the variable contents with a xpath expression?

Is it even possible to access the values like table:table-cell[$columnHeadings/heading[@name=&#39;Last_Name&#39;]]?


Answers to comments:
> Which XSLT processor are you using?

I'm using whatever libreoffice 7.4.5.1 is using.
Can I change that?
The xsl:vendor is "libxslt" and the version is "1.0" according to the &lt;xsl:value-of select=&quot;system-property(&#39;xsl:vendor&#39;)&quot;/&gt; and xsl:version values.

> Do you get an error on &lt;xsl:attribute name=&quot;name&quot; select=&quot;text:p&quot; /&gt;?

I actually do not for some reason. The test runs through without errors. I get a new browser tab with the produced xml output and no errors.

I tried ticking the "The filter needs XSLT 2.0 processor" but then I can't test run the filter anymore and don't get any output.

> What's the overall purpose of this exercise?

I want to be able to select the values in the columns by their respective column heading, instead of the index, because I want to make it as portable as possible. At least I think that would help to achieve that goal. I have 184 columns. The column names won't change as likely as the index of the column, I believe.

SOLVED

My working file looks like this now:

SuperBasicExportFilter.xslt

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet
version=&quot;1.0&quot;
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:table=&quot;urn:oasis:names:tc:opendocument:xmlns:table:1.0&quot;
xmlns:text=&quot;urn:oasis:names:tc:opendocument:xmlns:text:1.0&quot;
xmlns:office=&quot;urn:oasis:names:tc:opendocument:xmlns:office:1.0&quot;
xmlns:exsl=&quot;http://exslt.org/common&quot;
exclude-result-prefixes=&quot;table text office exsl&quot;
&gt;
&lt;xsl:output
method=&quot;xml&quot;
indent=&quot;yes&quot;
encoding=&quot;UTF-8&quot;
omit-xml-declaration=&quot;no&quot;
/&gt;
&lt;xsl:template match=&quot;/&quot;&gt;
&lt;xsl:variable name=&quot;columnHeadings-temp&quot; xmlns=&quot;&quot;&gt;
&lt;xsl:for-each select=&quot;//table:table/table:table-row[not(preceding::table:table-row)]//table:table-cell&quot;&gt;
&lt;xsl:element name=&quot;heading&quot;&gt;
&lt;xsl:attribute name=&quot;name&quot;&gt;
&lt;xsl value-of select=&quot;normalize-space(text:p)&quot; /&gt;
&lt;/xsl:attribute&gt;
&lt;xsl:value-of select=&quot;position()&quot; /&gt;
&lt;/xsl:element&gt;
&lt;/xsl:for-each&gt;
&lt;/xsl:variable&gt;
&lt;xsl:variable name=&quot;columnHeadings&quot; select=&quot;exsl:node-set($columnHeadings-temp)&quot; /&gt;
&lt;html&gt;
&lt;body&gt;
&lt;table&gt;
&lt;xsl:for-each select=&quot;//table:table/table:table-row&quot;&gt;
&lt;xsl:if test=&quot;position() &gt; 1&quot;&gt;
&lt;tr&gt;
&lt;td&gt;
First Column Value
&lt;xsl:value-of select=&quot;table:table-cell[number($columnHeadings/heading[@name=&#39;First_Name&#39;])]/text:p&quot; /&gt;
&lt;/td&gt;
&lt;td&gt;
Second Column Value
&lt;xsl:value-of select=&quot;table:table-cell[number($columnHeadings/heading[@name=&#39;Last_Name&#39;])]/text:p&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/xsl:if&gt;
&lt;/xsl:for-each&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

答案1

得分: 1

以下是翻译好的部分:

意图 &lt;xsl:attribute name=&quot;name&quot; select=&quot;text:p&quot; /&gt; 在 XSLT 1 中无法创建具有值的属性;它应该引发错误,但似乎您的 XSLT 处理器有点忽略了 select

所以尝试使用:

&lt;xsl:attribute name=&quot;name&quot;&gt;
&lt;xsl:value-of select=&quot;text:p&quot;/&gt;
&lt;/xsl:attribute&gt;

替代。

这样,我认为例如 &lt;xsl:variable name=&quot;columnHeadings-ns&quot; select=&quot;exsl:node-set($columnHeadings)&quot; xmlns:exsl=&quot;http://exslt.org/common&quot;/&gt; 应该允许您使用例如 &lt;xsl:value-of select=&quot;table:table-cell[$columnHeadings-ns/heading[@name=&#39;First_Name&#39;]]/text:p&quot;/&gt;

英文:

The intent &lt;xsl:attribute name=&quot;name&quot; select=&quot;text:p&quot; /&gt; fails to create an attribute with a value in XSLT 1; it should raise an error but it seems your XSLT processor kind of ignores the select.

So try

&lt;xsl:attribute name=&quot;name&quot;&gt;
&lt;xsl:value-of select=&quot;text:p&quot;/&gt;
&lt;/xsl:attribute&gt;

instead.

That way, I would think that e.g. &lt;xsl:variable name=&quot;columnHeadings-ns&quot; select=&quot;exsl:node-set($columnHeadings)&quot; xmlns:exsl=&quot;http://exslt.org/common&quot;/&gt; should allow you to use e.g. &lt;xsl:value-of select=&quot;table:table-cell[$columnHeadings-ns/heading[@name=&#39;First_Name&#39;]]/text:p&quot;/&gt;.

huangapple
  • 本文由 发表于 2023年2月19日 16:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498989.html
匿名

发表评论

匿名网友

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

确定