簡體   English   中英

如何使用xsl從XML中選擇一個元素?

[英]how can I select one Element from an XML with xsl?

嘿,我遇到了XML文件問題。 我在這里得到了一些幫助,但是之后我遇到了另一個問題。 我試圖選擇XML文件中的第一個元素,然后將其復制到另一個位置。

這是原始的:

<products>
     <product>
        <OrderingInfo>
            <item name="Part No.">12345 (text)</item>
            <item name="Part No.">12345IP (text)</item>
        </OrderingInfo>
        <varitems>
        </varitems>
    </product>

     <product>
        <OrderingInfo>
            <item name="Part No.">001 (text)</item>
            <item name="Part No.">002 (text)</item>
        </OrderingInfo>
        <varitems>
        </varitems>
     </product>
</products>

所需的輸出:

<products>
     <product>
        <OrderingInfo>
            <item name="Part No.">12345 (text)</item>
            <item name="Part No.">12345IP (text)</item>
        </OrderingInfo>
        <varitems>
            <item>
              <varno>14205</varno>
              <text>text</text>

            </item>
            <item>
              <varno>14205</varno>
              <text>text</text>
            </item>
        </varitems>
    </product>

     <product>
        <OrderingInfo>
            <item name="Part No.">001</item>
            <item name="Part No.">002</item>
        </OrderingInfo>
        <varitems>
            <item>
              <varno>001</varno>
              <text>text</text>
            </item>
            <item>
              <varno>001</varno>
              <text>text</text>
            </item>
        </varitems>
     </product>
</products>

我用它

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    </xsl:template>
      <xsl:template match="OrderingInfo">
        <xsl:copy-of select="."/>
        <varitems></varitems>
    </xsl:template>
    <xsl:template match="varitems">
        <xsl:copy>
            <xsl:for-each select="ancestor::product/OrderingInfo/item">
                <item>
                    <varno><xsl:value-of select="/*/product/OrderingInfo/item[1]"/></partno>
                    <vartitle><xsl:value-of select="normalize-space(translate(substring-after(current(),' '),'()',''))"/></vartitle>
                </item>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
 </xsl:stylesheet>

我試圖選擇帶有/*/product/OrderingInformation/item[1]部分的商品,但它始終選擇OrderingInfo的第一行並從第一商品中選擇

我認為您正在尋找的表達式是這個(因此,獲得當前節點的父item下的第一item )。

<varno>
   <xsl:value-of select="substring-before(../item[1], ' ')"/>
</varno>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM