【问题标题】:Using XPath to select nodes outside of the normal sequence of xsl:for-each使用 XPath 选择 xsl:for-each 正常序列之外的节点
【发布时间】:2014-10-23 14:12:42
【问题描述】:

我有以下 XML 代码:

<?xml version="1.0" encoding="UTF-8"?>
<Collection>
    <Content>
        <ID>12</ID>
        <Type>Content</Type>
        <Title>Office Location #1</Title>
        <QuickLink>/office.aspx?id=12</QuickLink>
        <Teaser>
            <p>
                <span class="infoBold">My Group</span>
                <br />
                WPO
                <br />
                Office Location #1
                <br />
                Wp, NY 090801
                <br />
                986.362.3265
            </p>
        </Teaser>
        <Html>
            <root>
                <Location>
                    <location />
                    <office>WPO</office>
                    <Address>
                        <image>
                            <img src="someing.png" />
                        </image>
                        <Address1>Office Location #1</Address1>
                        <Address2 />
                        <City>Wp</City>
                        <State>NY</State>
                        <zip>09081</zip>
                        <phone>986.362.3265</phone>
                        <fax />
                        <urgent_care_phone />
                    </Address>
                </Location>
            </root>
        </Html>
    </Content>
    <Content>
        <ID>48</ID>
        <Type>Content</Type>
        <Title>Office Location #3</Title>
        <QuickLink>/office.aspx?id=48</QuickLink>
        <Teaser>
            <p>
                <span class="infoBold">My Group</span>
                <br />
                WPO
                <br />
                Office Location #3
                <br />
                Wp, NY 090801
                <br />
                986.362.3265
            </p>
        </Teaser>
        <Html>
            <root>
                <Location>
                    <location />
                    <office>WPO</office>
                    <Address>
                        <image>
                            <img src="someing.png" />
                        </image>
                        <Address1>Office Location #3</Address1>
                        <Address2 />
                        <City>Wp</City>
                        <State>NY</State>
                        <zip>09081</zip>
                        <phone>986.362.3265</phone>
                        <fax />
                        <urgent_care_phone />
                    </Address>
                </Location>
            </root>
        </Html>
    </Content>
    <Content>
        <ID>36</ID>
        <Type>Content</Type>
        <Title>Office Location #2</Title>
        <QuickLink>/office.aspx?id=36</QuickLink>
        <Teaser>
            <p>
                <span class="infoBold">My Group</span>
                <br />
                WPO
                <br />
                Office Location #2
                <br />
                Wp, NY 090801
                <br />
                986.362.3265
            </p>
        </Teaser>
        <Html>
            <root>
                <Location>
                    <location>WP</location>
                    <office>WPO</office>
                    <Address>
                        <image>
                            <img src="someing.png" />
                        </image>
                        <Address1>Office Location #2</Address1>
                        <Address2 />
                        <City>Wp</City>
                        <State>NY</State>
                        <zip>09081</zip>
                        <phone>986.362.3265</phone>
                        <fax />
                        <urgent_care_phone />
                    </Address>
                </Location>
            </root>
        </Html>
    </Content>
</Collection>

我想显示如下格式:

FOR EACH ENTRY inside `Collection/Content/`:

    Html/root/Location/location (if not blank show it)
    Html/root/Location/Address {
        /Address1
        /Address2 (if not blank show it)
        /City
        /State
        /zip
        /phone
        /fax (if not blank show it)
    }

    Html/root/Location/location (if not blank show it)
    Html/root/Location/Address {
        /Address1
        /Address2 (if not blank show it)
        /City
        /State
        /zip
        /phone
        /fax (if not blank show it)
    }

    Html/root/Location/location (if not blank show it)
    Html/root/Location/Address {
        /Address1
        /Address2 (if not blank show it)
        /City
        /State
        /zip
        /phone
        /fax (if not blank show it)
    }

我正在尝试获取QuickLink

<xsl:for-each select="Collection/Content/Html">
    <div class="serviceHolder brClear">
        <xsl:value-of select="root/Location/Address/Address1" />
        <xsl:value-of select="root/Location/Address/Address2" />
        <xsl:value-of select="root/Location/Address/City" />
        <xsl:value-of select="root/Location/Address/State" />
        <xsl:value-of select="root/Location/Address/zip" />
        <xsl:value-of select="root/Location/Address/phone" />
        <a href="{QuickLink}" title="Test">Get Direction</a>
    </div>
</xsl:for-each>

虽然我在 for-each 中使用了Collection/Content/Html,但如何回到Collection/Content/QuickLink 获取此处的锚链接:

<a href="{QuickLink}" title="Test">Get Direction</a>

【问题讨论】:

  • 如果您想获得有用的响应,请确保您的所有代码都显示出来,并且您的预期输出正是您想要的。 (即,如果您想要 HTML,请编写 HTML,而不是花括号、括号和斜线的模糊混搭。)
  • 另外,请正确选择您的标题。这不是关于“如何使用 XSLT 向浏览器显示 XML”;这是关于与xsl:for-each 构造中/附近的代码相关的常规 XPath 选择。编辑为使用 XPath 选择 xsl:for-each 正常序列之外的节点
  • 谢谢。我觉得现在更有意义了。所以知道如何获得QuickLink
  • @Tomalak 我确实提供了一个例子来说明我想要做什么以及我请求帮助:)

标签: html xml xslt ektron


【解决方案1】:

对于a 生成问题,通过更改使QuickLink 的XPath 相对于xsl:for-each 内的上下文节点:

<a href="{QuickLink}" title="Test">Get Direction</a>

<a href="{../QuickLink}" title="Test">Get Direction</a>

至于通过为您编写 XSLT 在 HTML 中生成其余的伪标记,我更愿意帮助解决具体问题,而不是为您编写代码。如果您有任何其他具体问题,请告诉我们。

【讨论】:

  • 谢谢。这就是我需要知道的一切:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 2010-12-10
  • 2015-01-09
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
相关资源
最近更新 更多