需要使用XSLT 1.0将XML节点的文本分解为子节点

时间:2019-05-05 17:11:23

标签: xml xslt xslt-1.0

我正在使用Microsoft的XSLT处理器来处理XML块。 我需要使用XSLT 1.0分成一系列子节点。

输入非常规则,格式为“ ###,##:##>用户文本” 我认为我应该能够将前导数字和时间用作某种类型的标记/定界符。

一个例子可能是:

const addMovie = evt => {
 evt.preventDefault();
 setMovies(prevMovies => [
   ...prevMovies,
   { title: name, overview: "OK", id: movies.length + 1 }
 ]);
};

在这种情况下,有2个注释:

  1. 422,10:06>测试说明1
  2. 422,10:03>测试说明2

首位号码可以并且将有所不同。因此,它不能用作分隔符。我 Think 可以使用逗号和大于号来查找消息。

所需的输出是:

<xmldocument>
    <Notes>422, 10:06> Test Note 1 422, 10:03> Test Note 2 </Notes>
</xmldocument>

带有一个音符的示例:

<xmldocument>
    <Notes>
        <Note>
            <NoteTime>10:06</NoteTime>
            <NoteText>Test Note 1 (422)</NoteText>
        </Note>
        <Note>
            <NoteTime>10:03</NoteTime>
            <NoteText>Test Note 2 (422)</NoteText>
        </Note>
     </Notes>
</xmldocument>

将产生产量:

<xmldocument>
    <Notes>999, 10:06> Test Note 1</Notes>
</xmldocument>

还有一个带有3个注释的示例:

<xmldocument>
    <Notes>
        <Note>
            <NoteTime>10:06</NoteTime>
            <NoteText>Test Note 1 (999)</NoteText>
        </Note>
     </Notes>
</xmldocument>

将产生产量:

<xmldocument>
    <Notes>999, 10:06> Test Note 1 123, 10:08> Test Note 2 456, 10:10> Test Note 3</Notes>
</xmldocument>

我想我可以用this之类的东西来做到这一点,但在我看来,我不必为此增加级别复杂性。

1 个答案:

答案 0 :(得分:1)

---明确输入结构后进行编辑---

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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Notes">
    <xsl:copy>
        <xsl:call-template name="split-notes">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

<xsl:template name="split-notes">
    <xsl:param name="text"/>
    <xsl:variable name="num" select="substring-before($text, ', ')" />
    <xsl:variable name="rest" select="substring-after($text, ', ')" />
    <xsl:variable name="more" select="contains($rest, ', ')" />
    <xsl:variable name="note">
        <xsl:choose>
            <xsl:when test="$more">
                <xsl:call-template name="find-end">
                    <xsl:with-param name="text" select="substring-before($rest, ', ')"/>
                </xsl:call-template>        
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$rest"/>    
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <Note>
        <NoteTime>
            <xsl:value-of select="substring-before($note, '> ')"/>
        </NoteTime>
        <NoteText>
            <xsl:value-of select="substring-after($note, '> ')"/>  
            <xsl:text> (</xsl:text>
            <xsl:value-of select="$num"/>    
            <xsl:text>)</xsl:text>                                  
        </NoteText>
    </Note>
    <xsl:if test="$more">
        <!-- recursive call -->
        <xsl:call-template name="split-notes">
            <xsl:with-param name="text" select="substring-after($text, $note)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template name="find-end">
    <xsl:param name="text"/>
    <xsl:variable name="last-char" select="substring($text, string-length($text))"/>
    <xsl:choose>
        <xsl:when test="translate($last-char, '123456789', '000000000') = '0'">
            <!-- recursive call -->
            <xsl:call-template name="find-end">
                <xsl:with-param name="text" select="substring($text, 1, string-length($text) - 1)"/>
            </xsl:call-template>            
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>    
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

将其应用于以下输入时:

XML

<xmldocument>
    <Notes>999, 10:06> Test Note 1 123, 10:08> Test Note 2 456, 10:10> Test Note 3</Notes>
</xmldocument>

结果将是:

结果

<?xml version="1.0" encoding="UTF-8"?>
<xmldocument>
   <Notes>
      <Note>
         <NoteTime>10:06</NoteTime>
         <NoteText>Test Note 1  (999)</NoteText>
      </Note>
      <Note>
         <NoteTime>10:08</NoteTime>
         <NoteText>Test Note 2  (123)</NoteText>
      </Note>
      <Note>
         <NoteTime>10:10</NoteTime>
         <NoteText>Test Note 3 (456)</NoteText>
      </Note>
   </Notes>
</xmldocument>

演示https://xsltfiddle.liberty-development.net/ej9EGcB

相关问题