Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何在XSLT2.0中正确使用带有分析字符串组件的正则表达式_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

Xml 如何在XSLT2.0中正确使用带有分析字符串组件的正则表达式

Xml 如何在XSLT2.0中正确使用带有分析字符串组件的正则表达式,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,关于xslt 2.0转换和分析字符串组件,我有一个问题: 这就是我迄今为止所尝试的: <!-->Template for properties<--> <xsl:template match="UserValue"> <xsl:variable name="TITLE" select="./@title"/> <xsl:variable name="VALUE" select="./@value"/> <

关于xslt 2.0转换和分析字符串组件,我有一个问题:

这就是我迄今为止所尝试的:

<!-->Template for properties<-->
<xsl:template match="UserValue">

    <xsl:variable name="TITLE" select="./@title"/>
    <xsl:variable name="VALUE" select="./@value"/>

    <xsl:analyze-string select="$VALUE" regex="^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})$">

        <xsl:matching-substring>
            <Property>  
                <Title><xsl:value-of select="$TITLE"/></Title>
                <Value>Date:<xsl:value-of select="regex-group(1)"/>Time:<xsl:value-of select="regex-group(2)"/></Value>
            </Property>
        </xsl:matching-substring>

        <xsl:non-matching-substring>
            <!-->Set title and value property<-->
            <Property>
                <Title><xsl:value-of select="$TITLE"/></Title>
                <Value><xsl:value-of select="$VALUE"/></Value>
            </Property>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>
属性的
模板
日期:时间:
设置标题和值属性
有以下格式的日期时间字符串:YYYY-MM-DDTHH:MM:SS

我使用
^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})$
表达式来提取两个组。第一组带有日期,第二组带有时间,我想像在样式表中一样添加它们

现在发生的是,只执行非匹配子字符串块,而不执行匹配子字符串块。我还尝试了xsl when元素,在那里匹配工作,但这不是我想要的方式,而且我想使用
regex-group()
函数,因为它完全适合我的需要


我找不到我在这里可能犯的错误。提前谢谢你

选择
表示中使用大括号,这意味着将执行大括号内的表达式以获取值,而不是按字面意思输出。所以,实际上你的正则表达式被视为是这样的

^(\d4-\d2-\d2)T(\d2:\d2:\d2)$
要防止使用属性值模板,必须使用双大括号

<xsl:analyze-string select="$VALUE" regex="^(\d{{4}}-\d{{2}}-\d{{2}})T(\d{{2}}:\d{{2}}:\d{{2}})$">

选择
表示中使用大括号,这意味着将执行大括号内的表达式以获取值,而不是按字面意思输出。所以,实际上你的正则表达式被视为是这样的

^(\d4-\d2-\d2)T(\d2:\d2:\d2)$
要防止使用属性值模板,必须使用双大括号

<xsl:analyze-string select="$VALUE" regex="^(\d{{4}}-\d{{2}}-\d{{2}})T(\d{{2}}:\d{{2}}:\d{{2}})$">


现在你的问题已经被回答了,考虑一个完全不同的方法,利用XSLT 2的本土化能力:

  • 识别表示日期时间的字符串;及
  • 将日期时间强制转换为日期和/或时间
不需要正则表达式

<xsl:template match="UserValue">
    <Property>  
        <Title>
            <xsl:value-of select="@title"/>
        </Title>
        <Value>
            <xsl:choose>
                <xsl:when test="@value castable as xs:dateTime">
                    <xsl:variable name="dt" select="xs:dateTime(@value)" /> 
                    <xsl:text>Date:</xsl:text>  
                    <xsl:value-of select="xs:date($dt)" />
                    <xsl:text>Time:</xsl:text>  
                    <xsl:value-of select="xs:time($dt)" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="@value"/>
                </xsl:otherwise>
            </xsl:choose>
        </Value>
    </Property>
</xsl:template>

日期:
时间:


demo:

既然你的问题已经得到回答,考虑一个完全不同的方法,利用XSLT 2的本土化能力:

  • 识别表示日期时间的字符串;及
  • 将日期时间强制转换为日期和/或时间
不需要正则表达式

<xsl:template match="UserValue">
    <Property>  
        <Title>
            <xsl:value-of select="@title"/>
        </Title>
        <Value>
            <xsl:choose>
                <xsl:when test="@value castable as xs:dateTime">
                    <xsl:variable name="dt" select="xs:dateTime(@value)" /> 
                    <xsl:text>Date:</xsl:text>  
                    <xsl:value-of select="xs:date($dt)" />
                    <xsl:text>Time:</xsl:text>  
                    <xsl:value-of select="xs:time($dt)" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="@value"/>
                </xsl:otherwise>
            </xsl:choose>
        </Value>
    </Property>
</xsl:template>

日期:
时间:


演示:

请发布一个最小但完整的XML输入示例来演示问题。请发布一个最小但完整的XML输入示例来演示问题。感谢您的良好解释!这正好解决了问题!谢谢你的解释!这正好解决了问题!