0

如何使用 xsl:number 将当前元素属性的子字符串与前一个兄弟属性的子字符串的第一个实例匹配?

如果您查看下面的源 XML 文件,当 bookDocument 的 pages 属性的子字符串(字符 1 - 5)在进一步的 bookDocuments 中重复时,我打算在输出文件的 doc_code 属性中插入一个字母值(以“a”开头)其 pages 属性子字符串 1-5 与第一个匹配的每个后续 bookDocument。我一直在尝试使用 xsl:number 来获得所需的结果,但是使用 xsl:number 的“from”和“count”功能都没有成功。请参阅结果文件 resultdoc_5_12351.xml 的 doc_code 属性作为错误输出的示例。任何关于我做错了什么的有用建议将不胜感激。

源 XML 文件:

<book>
<bookGlossary>
<para>Here is a glossary.</para>
</bookGlossary>
<bookPart>
<bookChapter>
<title>Chapter 1</title>        

<bookDocument docnum='1' pages="12345,12346">
<para>This is the newDoc that should output a doc_code of 12345</para>              
</bookDocument>

<bookDocument docnum='2' pages="12346,12347,12348,12349,12350">
<para>This is the newDoc that should output a doc_code of 12346</para>              
</bookDocument>     

<bookDocument docnum='3' pages="12350,12351">
<para>This is the newDoc that should output a doc_code of 12350</para>              
</bookDocument>     

<bookDocument docnum='4' pages="12351">
<para>This is the newDoc that should output a doc_code of 12351</para>              
</bookDocument>     

<bookDocument docnum='5' pages="12351">
<para>This is the newDoc that should output a doc_code of 12351a</para>
</bookDocument>     

<bookDocument docnum='6' pages="12351,12352">
<para>This is the newDoc that should output a doc_code of 12351b</para>         
</bookDocument>

<bookDocument docnum='7' pages="12353">
<para>This is the newDoc that should output a doc_code of 12353</para>          
</bookDocument>

<bookDocument docnum='8' pages="12353">
<para>This is the newDoc that should output a doc_code of 12353a</para>         
</bookDocument> 

<bookDocument docnum='9' pages="12354">
<para>This is the newDoc that should output a doc_code of 12354</para>          
</bookDocument>

<bookDocument docnum='10' pages="12354, 12355">
<para>This is the newDoc that should output a doc_code of 12354a</para>         
</bookDocument> 

<bookDocument docnum='11' pages="12355">
<para>This is the newDoc that should output a doc_code of 12355</para>          
</bookDocument>

<bookDocument docnum='12' pages="12355">
<para>This is the newDoc that should output a doc_code of 12355a</para>         
</bookDocument>

<bookDocument docnum='13' pages="12356">
<para>This is the newDoc that should output a doc_code of 12356</para>          
</bookDocument>

</bookChapter>
</bookPart>
</book>

我当前的 XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="UTF-8"/>

<xsl:template match="/book">

<xsl:for-each select="bookPart/bookChapter/bookDocument">
<xsl:result-document href="resultdoc_{@docnum}_{@pages/substring(., 1, 5)}.xml">
<newDoc>
<docStart>

<xsl:attribute name="doc_code">
<xsl:choose>
<xsl:when test="preceding-sibling::bookDocument/@pages/substring(., 1, 5) = current()/@pages/substring(., 1, 5)">
<xsl:value-of select="@pages/substring(., 1, 5)"/>
<xsl:number level="any" from="bookDocument[preceding-sibling::bookDocument[@pages/substring(., 1, 5) = current()/@pages/substring(., 1, 5)]]" format="a"/></xsl:when>
<xsl:otherwise><xsl:value-of select="@pages/substring(., 1, 5) "/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>


</docStart>
<docBody>
<xsl:apply-templates select="*|text()"/>
</docBody>
</newDoc>
</xsl:result-document>
</xsl:for-each>
</xsl:template>

<xsl:template match="*|@*|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="text()">
<xsl:if test="normalize-space(.)">
<xsl:value-of select="normalize-space(.)"/>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

doc_code 属性值不正确的结果文档:

<!--resultdoc_1_12345.xml-->

<newDoc>
<docStart doc_code="12345"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12345</para>
</docBody>
</newDoc>

<!--resultdoc_2_12346.xml-->


<newDoc>
<docStart doc_code="12346"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12346</para>
</docBody>
</newDoc>

<!--resultdoc_3_12350.xml-->


<newDoc>
<docStart doc_code="12350"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12350</para>
</docBody>
</newDoc>


<!--resultdoc_4_12351.xml-->


<newDoc>
<docStart doc_code="12351"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12351</para>
</docBody>
</newDoc>

<!--resultdoc_5_12351.xml-->


<newDoc>
<docStart doc_code="12351e"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12351a</para>
</docBody>
</newDoc>

<!--resultdoc_6_12351.xml-->


<newDoc>
<docStart doc_code="12351b"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12351b</para>
</docBody>
</newDoc>


<!--resultdoc_7_12353.xml-->


<newDoc>
<docStart doc_code="12353"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12353</para>
</docBody>
</newDoc>

<!--resultdoc_8_12353.xml-->


<newDoc>
<docStart doc_code="12353c"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12353a</para>
</docBody>
</newDoc>

<!--resultdoc_9_12354.xml-->


<newDoc>
<docStart doc_code="12354"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12354</para>
</docBody>
</newDoc>

<!--resultdoc_10_12354.xml-->


<newDoc>
<docStart doc_code="12354c"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12354a</para>
</docBody>
</newDoc>

<!--resultdoc_11_12355.xml-->


<newDoc>
<docStart doc_code="12355"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12355</para>
</docBody>
</newDoc>

<!--resultdoc_12_12355.xml-->


<newDoc>
<docStart doc_code="12355c"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12355a</para>
</docBody>
</newDoc>

<!--resultdoc_13_12356.xml-->


<newDoc>
<docStart doc_code="12356"/>
<docBody>
<para>This is the newDoc that should output a doc_code of 12356</para>
</docBody>
</newDoc>
4

1 回答 1

0

我的第一印象是:

(a) 这可能超出了 xsl:number 的能力

(b) 你的 xsl:number 模式不起作用的原因是在模式中, current() 指的是与模式匹配的节点,而我认为你的代码假设它与被编号的节点匹配。

我会将其作为一个分组问题来解决(使用<xsl:for-each-group group-adjacent= "@pages/substring(., 1, 5)">; 您可以使用它<xsl:number value="position()" format="a">来获取和格式化代表 bookDocument 在其组中的位置的数字。

于 2013-08-27T21:12:53.437 回答