使用XPath选择XSL的正常顺序的之外的节点:换每个(Using XPath to select

2019-11-03 09:31发布

我有以下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>

虽然我使用的Collection/Content/Html中的for-each,我怎么回去Collection/Content/QuickLink这里的锚链接:

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

Answer 1:

对于a产生问题,使XPath来QuickLink相对于所述内上下文节点xsl:for-each通过改变:

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

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

至于由你书写XSLT生成HTML中的伪标记的其余部分,我宁愿以帮助特定的问题,而不是炮制出码为您服务。 让我们知道,如果您有任何其他具体问题。



文章来源: Using XPath to select nodes outside of the normal sequence of xsl:for-each