行计数包括转换节点
Row counting including transformed nodes
我需要 line
元素节点的正确行计数(在属性 pos
="1"、"2" 等中),该节点从其子元素进行部分转换。
XSLT 代码的当前 counting-part 无法正常工作。我还尝试创建一个变量模板来计算所需的节点,但到目前为止该变量是无用的,因为还不完全清楚如何在下一步中应用它。
来源XML
<?xml version="1.0" encoding="UTF-8"?>
<entry>
<line id="001" att1="aaa" att2="bbb" att3="ccc"/>
<line id="002" att1="ddd" att2="eee" att3="fff"/>
<line id="003" att1="ggg" att2="hhh" att3="iii">
<subline x="name" z="lastname"/>
<subline x="name2" z="lastname2"/>
<underline a="bar" b="foo"/>
</line>
</entry>
期望的输出(无论如何pos=
属性中的位置可以是任意的)
<?xml version="1.0" encoding="UTF-8"?><entry>
<entry>
<line pos="1" id="001" att1="aaa" att2="bbb" att3="ccc"/>
<line pos="2" id="002" att1="ddd" att2="eee" att3="fff"/>
<line pos="3" id="003" att1="ggg" att2="hhh" att3="iii" x="name" z="lastname"/>
<line pos="4" id="003" att1="ggg" att2="hhh" att3="iii" x="name2" z="lastname2"/>
<line pos="5" id="003" att1="ggg" att2="hhh" att3="iii" a="bar" b="foo"/>
</entry>
当前的 XSLT 代码
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="line[*]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<xsl:for-each select="entry/line">
<xsl:variable name="pos" select="position()" />
</xsl:for-each>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="line/*">
<line pos="{position()}">
<xsl:copy-of select="../@* | @*"/>
</line>
</xsl:template>
</xsl:stylesheet>
怎么样:
XSLT 1.0
<xsl:stylesheet version="1.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="/entry">
<xsl:copy>
<xsl:for-each select="line[not(*)] | line/*">
<line pos="{position()}">
<xsl:copy-of select="parent::line/@* | @*"/>
</line>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我需要 line
元素节点的正确行计数(在属性 pos
="1"、"2" 等中),该节点从其子元素进行部分转换。
XSLT 代码的当前 counting-part 无法正常工作。我还尝试创建一个变量模板来计算所需的节点,但到目前为止该变量是无用的,因为还不完全清楚如何在下一步中应用它。
来源XML
<?xml version="1.0" encoding="UTF-8"?>
<entry>
<line id="001" att1="aaa" att2="bbb" att3="ccc"/>
<line id="002" att1="ddd" att2="eee" att3="fff"/>
<line id="003" att1="ggg" att2="hhh" att3="iii">
<subline x="name" z="lastname"/>
<subline x="name2" z="lastname2"/>
<underline a="bar" b="foo"/>
</line>
</entry>
期望的输出(无论如何pos=
属性中的位置可以是任意的)
<?xml version="1.0" encoding="UTF-8"?><entry>
<entry>
<line pos="1" id="001" att1="aaa" att2="bbb" att3="ccc"/>
<line pos="2" id="002" att1="ddd" att2="eee" att3="fff"/>
<line pos="3" id="003" att1="ggg" att2="hhh" att3="iii" x="name" z="lastname"/>
<line pos="4" id="003" att1="ggg" att2="hhh" att3="iii" x="name2" z="lastname2"/>
<line pos="5" id="003" att1="ggg" att2="hhh" att3="iii" a="bar" b="foo"/>
</entry>
当前的 XSLT 代码
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="line[*]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<xsl:for-each select="entry/line">
<xsl:variable name="pos" select="position()" />
</xsl:for-each>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="line/*">
<line pos="{position()}">
<xsl:copy-of select="../@* | @*"/>
</line>
</xsl:template>
</xsl:stylesheet>
怎么样:
XSLT 1.0
<xsl:stylesheet version="1.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="/entry">
<xsl:copy>
<xsl:for-each select="line[not(*)] | line/*">
<line pos="{position()}">
<xsl:copy-of select="parent::line/@* | @*"/>
</line>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>