【问题标题】:Selecting attributes of nodes using for-each in XSLT在 XSLT 中使用 for-each 选择节点的属性
【发布时间】:2015-01-25 20:31:12
【问题描述】:

我有一个要求,我需要准备带有问题和答案列表的 XML。一个电子表格可以有多个文档。每个文档可以有多个问题。

我已经准备了两个相同的 XML。请建议哪一个是好方法。

XML 1

<?xml version="1.0" encoding="utf-8"?>
<Eform>
    <Documents>
        <Document ID="c7ba73bb-c096-4099-ad70-fd47d2320d00">
            <Questions>
                <Question ID="1a4eb1e0-f657-483e-a8fa-59e9002d7c3f" Title="Doc 1 - Question 1 - Paraghraph" Answer="abc" />
                <Question ID="d43fcf9d-91f6-43ce-b970-737aa0a51d36" Title="Doc 1 - Question 2 - Date" Answer="2014-10-08T18:30:00Z" />
            </Questions>
        </Document>
        <Document ID="12d726d9-6e77-4657-ab8c-dddb7104a14a">
            <Questions>
                <Question ID="3a9d6172-90b6-420d-911a-43857e7c21a2" Title="Doc 2 - Question 1 - SelectList" Answer="Option 03" />
                <Question ID="e4ae1e80-3fd5-4f6a-8d57-29166e399deb" Title="Doc 2 - Question 2 - File" Answer="" />
            </Questions>
        </Document>
        <Document ID="9e3e79d0-0417-4ecd-8d5f-1881a5045705">
            <Questions>
                <Question ID="3b6c296e-8cc3-4ba5-9b16-7b5d0bfa6ce0" Title="Doc 3 - Question 1 - Text" Answer="abc" />
            </Questions>
        </Document>
        <Document ID="328cae06-13c4-425e-adc0-e7c32d89b044">
            <Questions>
                <Question ID="d9877c67-12fc-4058-8dfe-3997335bce02" Title="Dropdown List" Answer="option 05" />
                <Question ID="a56aae48-94b8-43d7-b17e-5eb0d16aeebb" Title="CheckBox List" Answer="Option 03" />
                <Question ID="02ea17f1-8c34-4916-89ab-d6dfaf37a403" Title="RadioButton List" Answer="Option 03" />
            </Questions>
        </Document>
    </Documents>
</Eform>

XML 2

    <?xml version="1.0" encoding="utf-8"?>
<Eform>
    <Document ID="c7ba73bb-c096-4099-ad70-fd47d2320d00">
            <Question> 
                <ID>1a4eb1e0-f657-483e-a8fa-59e9002d7c3f</ID>
                <Title>Doc 1 - Question 1 - Paraghraph</Title>
                <Answer>abc</Answer>
            </Question> 
            <Question> 
                <ID>d43fcf9d-91f6-43ce-b970-737aa0a51d36</ID>
                <Title>Doc 1 - Question 2 - Date</Title>
                <Answer>2014-10-08T18:30:00Z</Answer>
            </Question> 
    </Document>
    <Document ID="12d726d9-6e77-4657-ab8c-dddb7104a14a">
            <Question> 
                <ID>3a9d6172-90b6-420d-911a-43857e7c21a2</ID>
                <Title>Doc 2 - Question 1 - SelectList</Title>
                <Answer>Option 03</Answer>
            </Question> 
            <Question> 
                <ID>e4ae1e80-3fd5-4f6a-8d57-29166e399deb</ID>
                <Title>Doc 2 - Question 2 - File</Title>
                <Answer></Answer>
            </Question> 
    </Document>
</Eform>

我想为上述任何一种 XML 生成 XSLT,以便生成 HTML,如下所示:

Question 1
Answer of Question 1

Question 2
Answer of Question 2

我第一次使用 XML 并尝试了一些东西。尝试为 XML2 如下。但没有给出所需的输出。

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
      <xsl:for-each select="Eform/Document">
 <xsl:for-each select="Question">
        <p><b><xsl:value-of select="Title"/></b></p>
        <p><xsl:value-of select="Answer"/></p>
        <br/>
 </xsl:for-each>
      </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

请建议正确的方法,以及如何使用 XSLT 中的嵌套 foreach 选择属性/值。让我知道是否有其他更好的方法可以做到这一点。

【问题讨论】:

  • 您应该显示您想要创建的确切 html。
  • 这将是一个简单的 html,其中一个接一个地列出了问题和答案。那它。 :)

标签: xml xslt xhtml


【解决方案1】:

对于您的第一种格式,请尝试:

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

<xsl:template match="/">
    <html>
        <body>
            <xsl:for-each select="Eform/Documents/Document/Questions/Question">
                <p><b><xsl:value-of select="@Title"/></b></p>
                <p><xsl:value-of select="@Answer"/></p>
                <br/>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

您的第二种格式格式不正确;如果是的话,这对它有用:

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

<xsl:template match="/">
    <html>
        <body>
            <xsl:for-each select="Eform/Document/Question">
                <p><b><xsl:value-of select="Title"/></b></p>
                <p><xsl:value-of select="Answer"/></p>
                <br/>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 我会选择第二个,可能会将问题的 ID 移动到一个属性中(就像文档一样)。见:ibm.com/developerworks/library/x-eleatt
  • 好的。谢谢。我也更改了问题中的 XML 2。但是您的第二个答案不起作用。
  • 正如我所说,您的 XML 需要格式正确。它适用于我,使用您的更正版本。
  • 哦..我检查了 XML 并且没有错误。我可以知道我到底错过了什么吗?
  • @Ajinkya 如果您的问题得到解答,请通过接受答案来关闭它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 2020-12-03
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多