如何访问 for-each 下的变量结构以在 XSLT 中创建 excel sheet
how to access variable structure under for-each to create excel sheet in XSLT
我的输入如下:
<item id="CON_1985_14">
<prv id="p1">
<no>1</no>
<text>This "May" take some moment, you "shall" not go there.</text>
<text>This is ok.</text>
</prv>
</item>
需要在引用值的基础上创建excel table。
=========================
A B
=========================
May CON_1985_14
shall CON_1985_14
=========================
创建 XSLT 以生成 excel 输出,例如:
<Worksheet ss:Name="R6">
<Table>
<Row>
<Cell>
<Data ss:Type="String">
<xsl:text>A</xsl:text>
</Data>
</Cell>
<Cell>
<Data ss:Type="String">
<xsl:text>B</xsl:text>
</Data>
</Cell>
</Row>
<xsl:for-each select="$path">
<xsl:for-each select="descendant::prv//descendant::text">
<xsl:variable name="STRUCTUREONBASISOFTOKENS" as="node()">
<Report>
<dd>
<xsl:for-each select="tokenize(normalize-space(.), '("|“)')">
<xsl:if test="(position() gt 1) and (position() mod 2 = 0)">
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="position() ne (last() - 1)">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>
</dd>
<LID>
<xsl:value-of select="/item/@id"/>
</LID>
</Report>
</xsl:variable>
<xsl:for-each select="tokenize($STRUCTUREONBASISOFTOKENS/Report/dd, ',')">
<Row>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="normalize-space(.)"/>
</Data>
</Cell>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="$STRUCTUREONBASISOFTOKENS/Report/LID"/>
</Data>
</Cell>
</Row>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</Table>
</Worksheet>
在 for-each 内部创建的变量在消息中打印输出,如:
<Report xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:LxNx="http://www.LXNX.com"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:dict="http://www.lexis-nexis.com/glp/dict" xmlns:ci="http://www.lexis-nexis.com/ci"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<dd>May, shall</dd>
<LID>CON_1985_14</LID>
</Report>
现在变量定义下面的嵌套 for-each 试图从变量结构中获取值以生成 ROW,但没有成功。
无法通过 xpath 从变量中获取值以生成行。
此语法 '' 表示 collection() 路径。
为此提供 suitable 解决方案。
我无法理解您尝试的 XSLT 的逻辑。考虑以下简化示例:
XML
<input>
<item>Lorem ipsum "alpha" dolor sit amet, consectetur "bravo" adipiscing elit "charlie".</item>
<item>"Delta" sed do eiusmod tempor "echo" incididunt ut labore et dolore magna aliqua.</item>
</input>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/input">
<table>
<xsl:for-each select="item">
<xsl:analyze-string select="." regex=""(.*?)"">
<xsl:matching-substring>
<row>
<cell>
<xsl:value-of select="regex-group(1)" />
</cell>
</row>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<table>
<row>
<cell>alpha</cell>
</row>
<row>
<cell>bravo</cell>
</row>
<row>
<cell>charlie</cell>
</row>
<row>
<cell>Delta</cell>
</row>
<row>
<cell>echo</cell>
</row>
</table>
我的输入如下:
<item id="CON_1985_14">
<prv id="p1">
<no>1</no>
<text>This "May" take some moment, you "shall" not go there.</text>
<text>This is ok.</text>
</prv>
</item>
需要在引用值的基础上创建excel table。
=========================
A B
=========================
May CON_1985_14
shall CON_1985_14
=========================
创建 XSLT 以生成 excel 输出,例如:
<Worksheet ss:Name="R6">
<Table>
<Row>
<Cell>
<Data ss:Type="String">
<xsl:text>A</xsl:text>
</Data>
</Cell>
<Cell>
<Data ss:Type="String">
<xsl:text>B</xsl:text>
</Data>
</Cell>
</Row>
<xsl:for-each select="$path">
<xsl:for-each select="descendant::prv//descendant::text">
<xsl:variable name="STRUCTUREONBASISOFTOKENS" as="node()">
<Report>
<dd>
<xsl:for-each select="tokenize(normalize-space(.), '("|“)')">
<xsl:if test="(position() gt 1) and (position() mod 2 = 0)">
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="position() ne (last() - 1)">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>
</dd>
<LID>
<xsl:value-of select="/item/@id"/>
</LID>
</Report>
</xsl:variable>
<xsl:for-each select="tokenize($STRUCTUREONBASISOFTOKENS/Report/dd, ',')">
<Row>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="normalize-space(.)"/>
</Data>
</Cell>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="$STRUCTUREONBASISOFTOKENS/Report/LID"/>
</Data>
</Cell>
</Row>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</Table>
</Worksheet>
在 for-each 内部创建的变量在消息中打印输出,如:
<Report xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:LxNx="http://www.LXNX.com"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:dict="http://www.lexis-nexis.com/glp/dict" xmlns:ci="http://www.lexis-nexis.com/ci"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<dd>May, shall</dd>
<LID>CON_1985_14</LID>
</Report>
现在变量定义下面的嵌套 for-each 试图从变量结构中获取值以生成 ROW,但没有成功。 无法通过 xpath 从变量中获取值以生成行。 此语法 '' 表示 collection() 路径。
为此提供 suitable 解决方案。
我无法理解您尝试的 XSLT 的逻辑。考虑以下简化示例:
XML
<input>
<item>Lorem ipsum "alpha" dolor sit amet, consectetur "bravo" adipiscing elit "charlie".</item>
<item>"Delta" sed do eiusmod tempor "echo" incididunt ut labore et dolore magna aliqua.</item>
</input>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/input">
<table>
<xsl:for-each select="item">
<xsl:analyze-string select="." regex=""(.*?)"">
<xsl:matching-substring>
<row>
<cell>
<xsl:value-of select="regex-group(1)" />
</cell>
</row>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<table>
<row>
<cell>alpha</cell>
</row>
<row>
<cell>bravo</cell>
</row>
<row>
<cell>charlie</cell>
</row>
<row>
<cell>Delta</cell>
</row>
<row>
<cell>echo</cell>
</row>
</table>