gpt4 book ai didi

xslt - 如何在必要的地方将序列属性转换为单个元素?

转载 作者:行者123 更新时间:2023-12-03 17:07:09 25 4
gpt4 key购买 nike

我必须使属性成为单个元素。请指导我。

这是我的意见和要求。

输入:

<book pagenum="01">
<title pagenum="01">Book title</title>
<chapter pagenum="02">
<chaptertitle pagenum="02">CHAPTER TITLE</chaptertitle>
<section pagenum="03">
<sectiontitle pagenum="03">SECTION TITLE</chaptertitle>
<subsection pagenum="04">
<para pagenum="04">body content</para>
<para pagenum="04">body content</para>
<para pagenum="04">body content</para>
<para pagenum="05">body content</para>
<para pagenum="05">body content</para>
<para pagenum="05">body content</para>
<para pagenum="06">body content</para>
<para pagenum="06">body content</para>
</subsection></section></chapter></book>


输出:

<book>
<?docpage num="01"?><pagenum id="p01">01</pagenum>
<booktitle>Book title</booktitle>
<chapter>
<?docpage num="02"?><pagenum id="p02">02</pagenum>
<chaptertitle>CHAPTER TITLE</chaptertitle>
<section>
<?docpage num="03"?><pagenum id="p03">03</pagenum>
<sectiontitle>SECTION TITLE</chaptertitle>
<subsection>
<?docpage num="04"?><pagenum id="p04">04</pagenum>
<para>body content</para>
<para>body content</para>
<para>body content</para>
<?docpage num="05"?><pagenum id="p05">05</pagenum>
<para>body content</para>
<para>body content</para>
<para>body content</para>
<?docpage num="06"?><pagenum id="p06">06</pagenum>
<para>body content</para>
<para>body content</para>
</subsection></section></chapter></book>


如何在XSLT脚本中转换它?请指导我。

谢谢,
米歇尔

最佳答案

遵循以下原则:

<!-- Identity transform - copy all elements as is by default -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>

<!-- Match any element for which @pagenum is different from preceding one.
Note that <para> is handled specially below. -->
<xsl:template match="*[@pagenum != preceding::*[1]/@pagenum]">
<xsl:copy>
<!-- Insert the PI and <pagenum> as first child -->
<xsl:call-template name="insert-pagenum"/>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>

<!-- Match <para> for which @pagenum is different from preceding one. -->
<xsl:template match="para[@pagenum != preceding::*[1]/@pagenum]">
<!-- Insert the PI and <pagenum> before the opening tag of <para> -->
<xsl:call-template name="insert-pagenum"/>
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>

<xsl:template name="insert-pagenum">
<xsl:processing-instruction name="docpage">
<xsl:text>num=</xsl:text><xsl:value-of select="@pagenum"/>
</xsl:processing-instruction>
<pagenum id="p{@pagenum}">
<xsl:value-of select="@pagenum"/>
</pagenum>
</xsl:template>

关于xslt - 如何在必要的地方将序列属性转换为单个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1339431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com