简体   繁体   English

XSLT 1.0:选择和复制元素

[英]XSLT 1.0: Selecting and Copying elements

I didn't have a lot of experience with XSLT and I'm struggling with the copy element.我对 XSLT 没有太多经验,而且我在复制元素上苦苦挣扎。 If someone could identify my mistake, it would be realy appreciated.如果有人能指出我的错误,将不胜感激。

I'm trying top split a specific node in an XML file.我正在尝试在 XML 文件中拆分特定节点。 Therefore, I match the node in question and use a "copy" within this node to match specific elements to reuse.因此,我匹配有问题的节点并在该节点中使用“副本”来匹配特定元素以重用。 The problem is that when achieving the copy, I get one <book> tag too much after <Pages> .问题是在实现副本时,我在<Pages>之后得到了一个太多的<book>标签。

INPUT XML:输入 XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<PartProducts>
   <PartProduct name="PRI"
                code="C"
                pubCode="PRI"
                pubDate="2021-01-26"
                modifiedDate="2021-01-26T18:06:26.0"
                zone="GP">
      <books>
         <book code="V" pageWidth="220" pageHeight="285" folios="1-2,131-132">
            <name FoliosFrontOK="2" FoliosBackOK="131">CV_Cover</name>
            <Pages>
               <Page id="PRICVGP21012600101" nr="1" fullPageAd="false" zone="GP">
                  <PublishedAds/>
               </Page>
               <Page id="PRICVGP21012600102" nr="2" fullPageAd="true" zone="GP">
                  <PublishedAds>
                     <Ad id="206937-538053-813296"
                         x="0.0"
                         y="0.0"
                         width="220"
                         height="285"
                         bleed="true"
                         cols=""
                         system=""
                         materialPath="adv_dpp_ads/Adbase/Ad/56/100207756-02.pdf"
                         uaid="0000207756-02"/>
                  </PublishedAds>
               </Page>
               <Page id="PRICVGP21012600103" nr="131" fullPageAd="true" zone="GP">
                  <PublishedAds>
                     <Ad id="206737-537110-812091"
                         x="0.0"
                         y="0.0"
                         width="220"
                         height="285"
                         bleed="true"
                         cols="2"
                         system="DPPPROD"
                         materialPath="adv_dpp_ads/Adbase/Ad/56/100207556-08.pdf"
                         uaid="0000207556-08"/>
                  </PublishedAds>
               </Page>
               <Page id="PRICVGP21012600104" nr="132" fullPageAd="true" zone="GP">
                  <PublishedAds>
                     <Ad id="206249-535610-810275"
                         x="0.0"
                         y="0.0"
                         width="220"
                         height="285"
                         bleed="true"
                         cols=""
                         system=""
                         materialPath="adv_dpp_ads/Adbase/Ad/67/100207067-01.pdf"
                         uaid="0000207067-01"/>
                  </PublishedAds>
               </Page>
            </Pages>
         </book>
      </books>
   </PartProduct>
   <PartProduct name="PRI"
                code="H"
                pubCode="PRI"
                pubDate="2021-01-26"
                modifiedDate="2021-01-26T18:06:26.0"
                zone="GP">
      <books></books>
   </PartProduct>
   <PartProduct name="PRI"
                code="H"
                pubCode="PRI"
                pubDate="2021-01-26"
                modifiedDate="2021-01-26T18:06:26.0"
                zone="GP">
      <books></books>
   </PartProduct>
</PartProducts>

OUTPUT XML OUTPUT XML

<?xml version="1.0" encoding="UTF-8"?>
<PartProducts>
   <PartProduct code="C"
                modifiedDate="2021-01-26T18:06:26.0"
                name="PRI"
                pubCode="PRI"
                pubDate="2021-01-26"
                zone="GP">
      <books>
         <book>
            <name>CV_Cover01</name>
            <Pages>
               <book>
                  <Page fullPageAd="false" id="PRICVGP21012600101" nr="1" zone="GP">
                     <PublishedAds/>
                  </Page>
                  <Page fullPageAd="true" id="PRICVGP21012600102" nr="2" zone="GP">
                     <PublishedAds>
                        <Ad bleed="true"
                            cols=""
                            height="285"
                            id="206937-538053-813296"
                            materialPath="adv_dpp_ads/Adbase/Ad/56/100207756-02.pdf"
                            system=""
                            uaid="0000207756-02"
                            width="220"
                            x="0.0"
                            y="0.0"/>
                     </PublishedAds>
                  </Page>
               </book>
            </Pages>
         </book>
         <book>
            <name>CV_Cover02</name>
            <Pages>
               <book>
                  <Page fullPageAd="true" id="PRICVGP21012600103" nr="131" zone="GP">
                     <PublishedAds>
                        <Ad bleed="true"
                            cols="2"
                            height="285"
                            id="206737-537110-812091"
                            materialPath="adv_dpp_ads/Adbase/Ad/56/100207556-08.pdf"
                            system="DPPPROD"
                            uaid="0000207556-08"
                            width="220"
                            x="0.0"
                            y="0.0"/>
                     </PublishedAds>
                  </Page>
                  <Page fullPageAd="true" id="PRICVGP21012600104" nr="132" zone="GP">
                     <PublishedAds>
                        <Ad bleed="true"
                            cols=""
                            height="285"
                            id="206249-535610-810275"
                            materialPath="adv_dpp_ads/Adbase/Ad/67/100207067-01.pdf"
                            system=""
                            uaid="0000207067-01"
                            width="220"
                            x="0.0"
                            y="0.0"/>
                     </PublishedAds>
                  </Page>
               </book>
            </Pages>
         </book>
      </books>
   </PartProduct>
   <PartProduct code="H"
                modifiedDate="2021-01-26T18:06:26.0"
                name="PRI"
                pubCode="PRI"
                pubDate="2021-01-26"
                zone="GP">
      <books></books>
   </PartProduct>
   <PartProduct code="H"
                modifiedDate="2021-01-26T18:06:26.0"
                name="PRI"
                pubCode="PRI"
                pubDate="2021-01-26"
                zone="GP">
      <books></books>
   </PartProduct>
</PartProducts>

XSLT XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tns="http://www.myproject/SS/PMSchema"
    exclude-result-prefixes="tns"
    version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="book[name='CV_Cover']">
    
    <xsl:variable name="Front" select="name/@FoliosFrontOK"/>
    <xsl:variable name="Back" select="name/@FoliosBackOK"/>
    
        <book>
            <name>CV_Cover01</name>
            <Pages>

            <xsl:copy>
                <xsl:apply-templates select="Pages/Page[@nr &lt;= $Front]" />
            </xsl:copy>
                        
            </Pages>
        </book>
        <book>
            <name>CV_Cover02</name>
            <Pages>

            <xsl:copy>
                <xsl:apply-templates select="Pages/Page[@nr >= $Back]" />
            </xsl:copy>
    
            </Pages>
        </book>
    </xsl:template>


    <!-- Select everything from original xml and copy it back + apply all templates -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

So, in the input xml we have books/book/Pages/Page and after the XSLT I get books/book/Pages/book/Page Any idea what I'm doing wrong, or how to get rid of this particular tag?因此,在输入 xml 中,我们有 books/book/Pages/Page,在 XSLT 之后,我得到 books/book/Pages/book/Page 知道我做错了什么,或者如何摆脱这个特定的标签?

The input xml contains pages 1, 2, 131 & 132输入 xml 包含第 1、2、131 和 132 页
The selector in your first copy statement matches against pages less than or equal to 2,第一个复制语句中的选择器与小于或等于 2 的页面匹配,
so it will return pages 1 and 2所以它将返回第 1 页和第 2 页
The selector in your second copy statement matches against pages greater than or equal to 131,第二个复制语句中的选择器匹配大于或等于 131 的页面,
so it will return pages 131 and 132所以它将返回第 131 和 132 页

From your comment in other answer not clearly shown in original post:根据您对原始帖子中未明确显示的其他答案的评论:

that's actually what I'm trying to do.这实际上就是我想要做的。 I'm trying to split the "CV_Cover" into 2 distinct node, one with the pages with folio <= 2 and the other one with the pages who have folio >= 131.我正在尝试将“CV_Cover”拆分为 2 个不同的节点,一个包含 folio <= 2 的页面,另一个包含 folio >= 131 的页面。

Consider splitting the child element, book into two inside the template of its parent, books , then process accordingly.考虑将子元素book在其父元素books的模板中拆分为两部分,然后进行相应的处理。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tns="http://www.myproject/SS/PMSchema"
    exclude-result-prefixes="tns"
    version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <!-- IDENTITY TRANSFORM -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="books[book/name='CV_Cover']">
      <xsl:copy>
        <xsl:variable name="Front" select="book/name/@FoliosFrontOK"/>
        <xsl:variable name="Back" select="book/name/@FoliosBackOK"/>
    
        <book>
            <xsl:apply-templates select="book/@*"/>
            <xsl:attribute name="folios">1-2</xsl:attribute>
            <name FoliosFrontOK="1" FoliosBackOK="2">CV_Cover01</name>
            <Pages>
                <xsl:apply-templates select="book/Pages/Page[@nr &lt;= $Front]" />
            </Pages>
        </book>

        <book>
            <xsl:apply-templates select="book/@*" />
            <xsl:attribute name="folios">131-132</xsl:attribute>
            <name FoliosFrontOK="131" FoliosBackOK="132">CV_Cover02</name>
            <Pages>
                <xsl:apply-templates select="book/Pages/Page[@nr &gt;= $Back]" />
            </Pages>
        </book>
      </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Online Demo 在线演示

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

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