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
Xml 获取XSLT中子节点的值_Xml_Xslt_Parent Child - Fatal编程技术网

Xml 获取XSLT中子节点的值

Xml 获取XSLT中子节点的值,xml,xslt,parent-child,Xml,Xslt,Parent Child,我对XML和XSLT非常陌生,但我正在尝试创建一个HTML页面,该页面通过不同长度的步骤循环并生成HTML列表 XML如下所示: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <module> <name ID="SDCModule002">

我对XML和XSLT非常陌生,但我正在尝试创建一个HTML页面,该页面通过不同长度的步骤循环并生成HTML列表

XML如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <module>
        <name ID="SDCModule002">
            <role>SDC</role>
            <modNum>002</modNum>
            <title>Test</title>
            <audience>me</audience>
            <numSteps>7</numSteps>
            <steps>
                <step1>Do this 1</step1>
                <step2>Do this 2</step2>
                <step3>Do this 3</step3>
                <step4>Do this 4</step4>
                <step5>Do this 5</step5>
                <step6>Do this 6</step6>
                <step7>Do this 7</step7>
            </steps>
        </name>
        <name ID="SDCModule003">
            <role>SDC</role>
            <modNum>003</modNum>
            <title>Hi</title>
            <audience>you</audience>
            <numSteps>4</numSteps>
            <steps>
                <step1>Hi</step1>
                <step2>There</step2>
                <step3>You</step3>
                <step4>Guys</step4>
            </steps>
        </name>
    </module>
</xml>

SDC
002
试验
我
7.
这样做1
做这个2
做这个3
这样做4
做这个5
这样做6
这样做7
SDC
003
你好
你
4.
你好
那里
你
伙计们
我有它这样的信息,如标题,观众,任务等显示的模块,我正在搜索,以便工作

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/xml">
<html>
<head>
<title><xsl:value-of select="module/name[@ID='SDCModule001']/title "/></title>
</head>

<xsl:apply-templates select="module/name[@ID='SDCModule001']"/>

</body> 
</html> 
</xsl:template> 

<xsl:template name="stepList" match="name">


<div id= "info">
<center>

<font face="verdana" size="2"><b><center><xsl:value-of select="title" /></center></b></font>
<hr></hr>

<xsl:value-of select="audience" />
<p></p>
<xsl:value-of select="numSteps" />

</center>

</div>

</xsl:template>


</xsl:stylesheet>


我想我需要一个
在一个有序列表中列出步骤,这样SDCModule002将有7个步骤,而SDCModule003将只有4个步骤,还是我只需要得到父节点
的所有子节点的值

我不想做15个if语句,说“如果numSteps是1,那么做这个……如果numSteps是2,那么做那个”等等。。。我可以,而且它会起作用,但我不确定是否有更有效的方法来做到这一点

已编辑

预期结果如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <module>
        <name ID="SDCModule002">
            <role>SDC</role>
            <modNum>002</modNum>
            <title>Test</title>
            <audience>me</audience>
            <numSteps>7</numSteps>
            <steps>
                <step1>Do this 1</step1>
                <step2>Do this 2</step2>
                <step3>Do this 3</step3>
                <step4>Do this 4</step4>
                <step5>Do this 5</step5>
                <step6>Do this 6</step6>
                <step7>Do this 7</step7>
            </steps>
        </name>
        <name ID="SDCModule003">
            <role>SDC</role>
            <modNum>003</modNum>
            <title>Hi</title>
            <audience>you</audience>
            <numSteps>4</numSteps>
            <steps>
                <step1>Hi</step1>
                <step2>There</step2>
                <step3>You</step3>
                <step4>Guys</step4>
            </steps>
        </name>
    </module>
</xml>
模块:SCDModule001 标题:测试 观众:我 步骤: 1.这样做1 2.做这个2 3.做这个3 4.这样做4 6.做这个5 7.这样做6
8.执行此操作7

步骤上方为每个
合并一个
xsl:for?
元素可以如下所示。我还更改了
xsl:apply templates
中的一些内容,以在输出中显示所有
module/name
s。输出并不漂亮,但应该给您一个良好的开端。别忘了添加一个

<?xml-stylesheet href="test.xslt" type="text/xsl" ?>

行到XML以在浏览器中运行它

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="text()" />

<xsl:template match="/xml">
    <html>
        <head>
            <title>***<xsl:value-of select="module/name[@ID='SDCModule002']/title "/>***</title>
        </head>
        <body>
            <xsl:apply-templates select="module/name"/>
        </body> 
    </html> 
</xsl:template> 

<xsl:template match="steps">
    <div id= "info">
        <center>
            <font face="verdana" size="2">
                <b>
                    <center>
                        <xsl:value-of select="../title" />
                    </center>
                </b>
            </font>
            <hr />
            <xsl:value-of select="../audience" />
            <p></p>
            <xsl:value-of select="../numSteps" />
            <ol>
                <xsl:for-each select="*">
                    <li><xsl:value-of select="." /></li>
                </xsl:for-each>
            </ol>
        </center>
    </div>
</xsl:template>

</xsl:stylesheet>

******

  • 在Firefox中,输出如下所示:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <module>
            <name ID="SDCModule002">
                <role>SDC</role>
                <modNum>002</modNum>
                <title>Test</title>
                <audience>me</audience>
                <numSteps>7</numSteps>
                <steps>
                    <step1>Do this 1</step1>
                    <step2>Do this 2</step2>
                    <step3>Do this 3</step3>
                    <step4>Do this 4</step4>
                    <step5>Do this 5</step5>
                    <step6>Do this 6</step6>
                    <step7>Do this 7</step7>
                </steps>
            </name>
            <name ID="SDCModule003">
                <role>SDC</role>
                <modNum>003</modNum>
                <title>Hi</title>
                <audience>you</audience>
                <numSteps>4</numSteps>
                <steps>
                    <step1>Hi</step1>
                    <step2>There</step2>
                    <step3>You</step3>
                    <step4>Guys</step4>
                </steps>
            </name>
        </module>
    </xml>
    

    步骤?
    元素上方为每个
    元素合并一个
    xsl:。我还更改了
    xsl:apply templates
    中的一些内容,以在输出中显示所有
    module/name
    s。输出并不漂亮,但应该给您一个良好的开端。别忘了添加一个

    <?xml-stylesheet href="test.xslt" type="text/xsl" ?>
    
    
    
    行到XML以在浏览器中运行它

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="text()" />
    
    <xsl:template match="/xml">
        <html>
            <head>
                <title>***<xsl:value-of select="module/name[@ID='SDCModule002']/title "/>***</title>
            </head>
            <body>
                <xsl:apply-templates select="module/name"/>
            </body> 
        </html> 
    </xsl:template> 
    
    <xsl:template match="steps">
        <div id= "info">
            <center>
                <font face="verdana" size="2">
                    <b>
                        <center>
                            <xsl:value-of select="../title" />
                        </center>
                    </b>
                </font>
                <hr />
                <xsl:value-of select="../audience" />
                <p></p>
                <xsl:value-of select="../numSteps" />
                <ol>
                    <xsl:for-each select="*">
                        <li><xsl:value-of select="." /></li>
                    </xsl:for-each>
                </ol>
            </center>
        </div>
    </xsl:template>
    
    </xsl:stylesheet>
    
    
    ******
    

  • 在Firefox中,输出如下所示:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <module>
            <name ID="SDCModule002">
                <role>SDC</role>
                <modNum>002</modNum>
                <title>Test</title>
                <audience>me</audience>
                <numSteps>7</numSteps>
                <steps>
                    <step1>Do this 1</step1>
                    <step2>Do this 2</step2>
                    <step3>Do this 3</step3>
                    <step4>Do this 4</step4>
                    <step5>Do this 5</step5>
                    <step6>Do this 6</step6>
                    <step7>Do this 7</step7>
                </steps>
            </name>
            <name ID="SDCModule003">
                <role>SDC</role>
                <modNum>003</modNum>
                <title>Hi</title>
                <audience>you</audience>
                <numSteps>4</numSteps>
                <steps>
                    <step1>Hi</step1>
                    <step2>There</step2>
                    <step3>You</step3>
                    <step4>Guys</step4>
                </steps>
            </name>
        </module>
    </xml>
    

    根据经验,在不需要时,应该避免使用xsl:for-each。尽管对于小型翻译或项目来说,性能方面的问题并不重要。应用模板的附加好处是,如果在实现过程中输入发生了更改,则只需更改XPath

    下面的示例为每个模块创建2个html文件。与“步骤”匹配的模板是一个额外的模板,您不需要它来使其工作

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/xml">
        <xsl:apply-templates select="module/name"/>
    </xsl:template>
    <!--Each name is a file - but can easily changed to module -->
    <xsl:template match="name">
        <html>
            <head>
                <title>***<xsl:value-of select="@ID"/>***</title>
            </head>
            <body>
                <div id= "info">
                    <center>
                        <font face="verdana" size="2">
                            <b>
                                <center>
                                    <xsl:value-of select="title" />
                                </center>
                            </b>
                        </font>
                        <hr />
                        <xsl:value-of select="audience" />
                        <p></p>
                        <xsl:apply-templates select="steps"/>
                    </center>
                </div>
            </body>
        </html>
    </xsl:template>
    
    <!--Extra match for styling - steps can also directly be called from "name" template-->
    <xsl:template match="steps">
        <xsl:value-of select="numSteps" />
        <ol>
            <xsl:apply-templates select="node()" mode="steps"/>
        </ol>
    </xsl:template>
    
    <!--Wildcard - matches all step nodes-->
    <xsl:template match="*" mode="steps">
        <li><xsl:value-of select="." /></li>
    </xsl:template>
    
    
    ******
    


  • 根据经验,在不需要时,应该避免使用xsl:for-each。尽管对于小型翻译或项目来说,性能方面的问题并不重要。应用模板的附加好处是,如果在实现过程中输入发生了更改,则只需更改XPath

    下面的示例为每个模块创建2个html文件。与“步骤”匹配的模板是一个额外的模板,您不需要它来使其工作

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/xml">
        <xsl:apply-templates select="module/name"/>
    </xsl:template>
    <!--Each name is a file - but can easily changed to module -->
    <xsl:template match="name">
        <html>
            <head>
                <title>***<xsl:value-of select="@ID"/>***</title>
            </head>
            <body>
                <div id= "info">
                    <center>
                        <font face="verdana" size="2">
                            <b>
                                <center>
                                    <xsl:value-of select="title" />
                                </center>
                            </b>
                        </font>
                        <hr />
                        <xsl:value-of select="audience" />
                        <p></p>
                        <xsl:apply-templates select="steps"/>
                    </center>
                </div>
            </body>
        </html>
    </xsl:template>
    
    <!--Extra match for styling - steps can also directly be called from "name" template-->
    <xsl:template match="steps">
        <xsl:value-of select="numSteps" />
        <ol>
            <xsl:apply-templates select="node()" mode="steps"/>
        </ol>
    </xsl:template>
    
    <!--Wildcard - matches all step nodes-->
    <xsl:template match="*" mode="steps">
        <li><xsl:value-of select="." /></li>
    </xsl:template>
    
    
    ******
    


  • 您的输入中没有
    SCDModule001
    。您好,是的,我删除了它,因为它很长(XML代码中有15个步骤),但在我的原始版本中,它就在那里。我选择002和003是因为它们比较短。。。。很抱歉让您困惑。您的输入中没有
    SCDModule001
    。您好,是的,我删除了它,因为它很长(XML代码中有15个步骤),但在我的原始版本中,它就在那里。我选择002和003是因为它们比较短。。。。对不起,把你弄糊涂了。谢谢。这是一个很好的开始。我想把每一个都放在一个单独的页面上,所以我想把所有的SCDModule001放在一个页面上,而不是两个都显示出来,但我想我能解决这个问题。这正是我要找的!非常感谢。这是一个很好的开始。我想把每一个都放在一个单独的页面上,所以我想把所有的SCDModule001放在一个页面上,而不是两个都显示出来,但我想我能解决这个问题。这是我的前任