Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
Json XSLT中兄弟选择的问题_Json_Xml_Xslt_Xpath_Siblings - Fatal编程技术网

Json XSLT中兄弟选择的问题

Json XSLT中兄弟选择的问题,json,xml,xslt,xpath,siblings,Json,Xml,Xslt,Xpath,Siblings,因此,我有一个XML文档,大致如下所示: <root> <section> <text>A</text> <alt> <text>1</text> </alt> <text>B</text> <nest> <text>C

因此,我有一个XML文档,大致如下所示:

<root>
    <section>
        <text>A</text>
        <alt>
            <text>1</text>
        </alt>
        <text>B</text>
        <nest>
            <text>C</text>
            <alt>
                <text>3</text>
            </alt>
            <text>D</text>
        </nest>
        <text>E</text>
        <alt>
            <text>4</text>
            <text>5</text>
        </alt>
    </section>
</root>
也就是说,
nest
标记存在时,它的函数基本上是空的。我已经使用以下XSLT脚本完成了大部分转换:

<xsl:template match="root">
    <xsl:text>[</xsl:text>
        <xsl:apply-templates select=".//section/item|.//section/nest/item"/>
    <xsl:text>]</xsl:text>
</xsl:template>

<xsl:template match="section/item|section/nest/item">
    <xsl:text>{</xsl:text>
        <xsl:text>"text":"</xsl:text>
            <xsl:value-of select="current()"/>
        <xsl:text>"</xsl:text>

        <xsl:if test="following-sibling::alt">
            <xsl:text>, "alternate":"</xsl:text>
                <xsl:apply-templates select="alt"/>
            <xsl:text>"</xsl:text>
        </xsl:if>

        <xsl:text>}</xsl:text>
    <xsl:if test="position() != last()">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:template>


<xsl:template match="alt">
    <xsl:for-each select="text">
        <xsl:value-of select="current()"/>
        <xsl:if test="position() != last()">
            <xsl:text>;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

[
]
{
“文本”:
"
“候补”:
"
}
,
;
它运行,但实际上无法识别
alt
元素。我假设这个测试中有一些东西:
不太正确,但我无法找出它来挽救我的生命

我尝试过其他一些分组,但这是我得到的最接近功能的版本。我主要是想弄清楚如何让这个兄弟测试和遍历工作,但是我在XSLT方面的专业水平非常低,所以我可能只是从错误的角度来处理问题


首选XSLT 1.0。

您的XSLT与示例XML输入不匹配。给定示例输入,可使用以下方法实现所需输出:

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/root">
    <xsl:text>[</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>&#10;]</xsl:text>
</xsl:template>

<xsl:template match="text">
    <xsl:text>&#10;&#9;{"text":"</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>"</xsl:text>
    <xsl:apply-templates select="following-sibling::*[1][self::alt]" mode="alt"/>
    <xsl:text>}</xsl:text>
</xsl:template>
  
<xsl:template match="alt" mode="alt">
    <xsl:text>, "alternate":"</xsl:text>
    <xsl:apply-templates mode="alt"/>
    <xsl:text>"</xsl:text>
</xsl:template>

<xsl:template match="alt/text" mode="alt">
    <xsl:value-of select="."/>
    <xsl:if test="position()!=last()">;</xsl:if>
</xsl:template>

<xsl:template match="alt/text"/>

</xsl:stylesheet>

[

]

	{“案文”:
"
}
“候补”:
"
;
或者更简单地说:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/root">
    <xsl:text>[</xsl:text>
    <xsl:for-each select="//text[not(parent::alt)]">
        <xsl:text>&#10;&#9;{"text":"</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"</xsl:text>
        <xsl:variable name="alt" select="following-sibling::*[1][self::alt]"/>
        <xsl:if test="$alt">
            <xsl:text>, "alternate":"</xsl:text>
            <xsl:for-each select="$alt/text">
                <xsl:value-of select="."/>
                <xsl:if test="position()!=last()">;</xsl:if>
            </xsl:for-each>
            <xsl:text>"</xsl:text>
        </xsl:if>   
        <xsl:text>}</xsl:text>
    </xsl:for-each>
    <xsl:text>&#10;]</xsl:text>
</xsl:template>

</xsl:stylesheet>

[

、 	;{“文本”:
"
“候补”:
;
"
}

]

您的XSLT与示例XML输入不匹配。给定示例输入,可使用以下方法实现所需输出:

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/root">
    <xsl:text>[</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>&#10;]</xsl:text>
</xsl:template>

<xsl:template match="text">
    <xsl:text>&#10;&#9;{"text":"</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>"</xsl:text>
    <xsl:apply-templates select="following-sibling::*[1][self::alt]" mode="alt"/>
    <xsl:text>}</xsl:text>
</xsl:template>
  
<xsl:template match="alt" mode="alt">
    <xsl:text>, "alternate":"</xsl:text>
    <xsl:apply-templates mode="alt"/>
    <xsl:text>"</xsl:text>
</xsl:template>

<xsl:template match="alt/text" mode="alt">
    <xsl:value-of select="."/>
    <xsl:if test="position()!=last()">;</xsl:if>
</xsl:template>

<xsl:template match="alt/text"/>

</xsl:stylesheet>

[

]

	{“案文”:
"
}
“候补”:
"
;
或者更简单地说:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/root">
    <xsl:text>[</xsl:text>
    <xsl:for-each select="//text[not(parent::alt)]">
        <xsl:text>&#10;&#9;{"text":"</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"</xsl:text>
        <xsl:variable name="alt" select="following-sibling::*[1][self::alt]"/>
        <xsl:if test="$alt">
            <xsl:text>, "alternate":"</xsl:text>
            <xsl:for-each select="$alt/text">
                <xsl:value-of select="."/>
                <xsl:if test="position()!=last()">;</xsl:if>
            </xsl:for-each>
            <xsl:text>"</xsl:text>
        </xsl:if>   
        <xsl:text>}</xsl:text>
    </xsl:for-each>
    <xsl:text>&#10;]</xsl:text>
</xsl:template>

</xsl:stylesheet>

[

、 	;{“文本”:
"
“候补”:
;
"
}

]

在所有有关XSLT的问题中,请说明您的处理器支持哪个版本的XSLT。您的样式表引用了一个名为
ch
的元素,该元素在您的输入中不存在,因此基本上没有意义。@MichaelKay啊,抱歉。我正在做一些翻译,我错过了其中一个
ch
item
。在所有有关XSLT的问题中,请说明您的处理器支持哪个版本的XSLT。您的样式表引用了一个名为
ch
的元素,该元素在您的输入中不存在,因此基本上没有意义。@MichaelKay啊,抱歉。我正在做一些翻译,我错过了其中一个<代码>目录是