简体   繁体   English

使用XPath选择xsl:for-each常规序列之外的节点

[英]Using XPath to select nodes outside of the normal sequence of xsl:for-each

I have the following XML code: 我有以下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>

I want to display the following format: 我想显示以下格式:

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)
    }

I am trying to get the QuickLink 我正在尝试获取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>

Although I am using the Collection/Content/Html in the for-each, how do I go back to Collection/Content/QuickLink for the anchor link here: 尽管我在for-each中使用了Collection/Content/Html ,但是如何在此处返回锚点链接的Collection/Content/QuickLink

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

For the a generation problem, make the XPath to QuickLink relative to the context node within the xsl:for-each by changing: 对于a产生问题,使XPath来QuickLink相对于所述内上下文节点xsl:for-each通过改变:

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

to

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

As for generating the rest of your pseudo markup in HTML by writing XSLT for you, I'd prefer to help with specific question rather than crank out code for you. 至于通过为您编写XSLT在HTML中生成其余的伪标记,我宁愿为特定问题提供帮助,而不是为您编写代码。 Let us know if you have any other specific questions. 如果您还有其他具体问题,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM