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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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 在xsl标记化中,如何获取前面的文本值_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

Xml 在xsl标记化中,如何获取前面的文本值

Xml 在xsl标记化中,如何获取前面的文本值,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,请建议如何在tokenize中获取前面的文本。在此示例中,将应用基于文本的web属性。例如,如果“SKY1996”键入前面带有字符串“Moon”的文本,则属性值为“Moon”,否则为“Sun” XML: <article> <text1>The solar eclipse Sun: SKY1996 is happenned in 1996</text1> <text2>The moon eclipse Moon: SKY1997 is happ

请建议如何在tokenize中获取前面的文本。在此示例中,将应用基于文本的web属性。例如,如果“SKY1996”键入前面带有字符串“Moon”的文本,则属性值为“Moon”,否则为“Sun”

XML:

<article>
 <text1>The solar eclipse Sun: SKY1996 is happenned in 1996</text1>
 <text2>The moon eclipse Moon: SKY1997 is happenned in 1997</text2>
</article>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="text1/text()|text2/text()">
        <xsl:for-each select="tokenize(., ' ')">
            <xsl:variable name="varPrecededText"><xsl:value-of select=".[preceding-sibling::node()[1]]"/></xsl:variable><!--Within tokenize, preceded text value --><!--Requesting suggestion to get this -->
            <xsl:choose>
                <xsl:when test="matches(., '^([S][K][Y])\d{4}$')">
                    <xsl:element name="web">
                        <xsl:attribute name="href">
                            <xsl:choose>
                                <xsl:when test="contains($varPrecededText, 'Moon')">
                                    <xsl:text>MoonEclipse:</xsl:text>
                                </xsl:when>
                                <xsl:otherwise><xsl:text>SunEclipse:</xsl:text></xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="."/>
                        </xsl:attribute>
                        <xsl:value-of select="."/>
                    </xsl:element><xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                    <xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
<article>
<text1>The solar eclipse Sun: <web href="SunEclipse:SKY1996">SKY1996</web> is happenned in 1996</text1>
<text2>The moon eclipse Moon: <web href="MoonEclipse:SKY1997">SKY1997</web> is happenned in 1997</text2>
</article>

日食太阳:天空1996发生在1996年
月食月亮:天空1997发生在1997年
XSLT2.0:

<article>
 <text1>The solar eclipse Sun: SKY1996 is happenned in 1996</text1>
 <text2>The moon eclipse Moon: SKY1997 is happenned in 1997</text2>
</article>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="text1/text()|text2/text()">
        <xsl:for-each select="tokenize(., ' ')">
            <xsl:variable name="varPrecededText"><xsl:value-of select=".[preceding-sibling::node()[1]]"/></xsl:variable><!--Within tokenize, preceded text value --><!--Requesting suggestion to get this -->
            <xsl:choose>
                <xsl:when test="matches(., '^([S][K][Y])\d{4}$')">
                    <xsl:element name="web">
                        <xsl:attribute name="href">
                            <xsl:choose>
                                <xsl:when test="contains($varPrecededText, 'Moon')">
                                    <xsl:text>MoonEclipse:</xsl:text>
                                </xsl:when>
                                <xsl:otherwise><xsl:text>SunEclipse:</xsl:text></xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="."/>
                        </xsl:attribute>
                        <xsl:value-of select="."/>
                    </xsl:element><xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                    <xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
<article>
<text1>The solar eclipse Sun: <web href="SunEclipse:SKY1996">SKY1996</web> is happenned in 1996</text1>
<text2>The moon eclipse Moon: <web href="MoonEclipse:SKY1997">SKY1997</web> is happenned in 1997</text2>
</article>

月食:
日食:

所需结果:

<article>
 <text1>The solar eclipse Sun: SKY1996 is happenned in 1996</text1>
 <text2>The moon eclipse Moon: SKY1997 is happenned in 1997</text2>
</article>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="text1/text()|text2/text()">
        <xsl:for-each select="tokenize(., ' ')">
            <xsl:variable name="varPrecededText"><xsl:value-of select=".[preceding-sibling::node()[1]]"/></xsl:variable><!--Within tokenize, preceded text value --><!--Requesting suggestion to get this -->
            <xsl:choose>
                <xsl:when test="matches(., '^([S][K][Y])\d{4}$')">
                    <xsl:element name="web">
                        <xsl:attribute name="href">
                            <xsl:choose>
                                <xsl:when test="contains($varPrecededText, 'Moon')">
                                    <xsl:text>MoonEclipse:</xsl:text>
                                </xsl:when>
                                <xsl:otherwise><xsl:text>SunEclipse:</xsl:text></xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="."/>
                        </xsl:attribute>
                        <xsl:value-of select="."/>
                    </xsl:element><xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                    <xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
<article>
<text1>The solar eclipse Sun: <web href="SunEclipse:SKY1996">SKY1996</web> is happenned in 1996</text1>
<text2>The moon eclipse Moon: <web href="MoonEclipse:SKY1997">SKY1997</web> is happenned in 1997</text2>
</article>

日食太阳:天空1996发生在1996年
月食月亮:天空1997发生在1997年

我只需使用分析字符串:

<xsl:template match="text1/text()|text2/text()">
    <xsl:analyze-string select="." regex="((\w+):)\s+([S][K][Y]\d{{4}})">
        <xsl:matching-substring>
            <web href="{regex-group(2)}Eclipse:{regex-group(3)}">
                <xsl:value-of select="regex-group(3)"/>
            </web>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:value-of select="."/>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>


(\w+)\s+([s][K][Y]\d{4}}
捕获第2组的
Sun:SKY1996
Moon:SKY1997
Sun
Moon
,第3组的
SKY1996
SKY1997
。这些匹配在xsl:matching子字符串中处理,以用所需的结构替换它们。非匹配项保留原样。

谢谢您的宝贵建议,先生,还有一个。