【问题标题】:XSLT 1.0: Selecting and Copying elementsXSLT 1.0:选择和复制元素
【发布时间】:2021-05-05 15:27:02
【问题描述】:

我没有太多使用 XSLT 的经验,而且我正在为复制元素而苦苦挣扎。 如果有人能指出我的错误,将不胜感激。

我正在尝试从顶部拆分 XML 文件中的特定节点。 因此,我匹配有问题的节点并在该节点内使用“副本”来匹配特定元素以重用。 问题是实现复制的时候,<Pages>后面多了一个<book>标签。

输入 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>

输出 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

<?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>

因此,在输入 xml 中,我们有 books/book/Pages/Page,在 XSLT 之后,我得到 books/book/Pages/book/Page 知道我做错了什么,或者如何摆脱这个特定的标签吗?

【问题讨论】:

  • 最后一个问题,什么是特定标签,因为您显示了许多标签?请显示所需的 XML 结果。

标签: xml xslt xslt-1.0


【解决方案1】:

根据您对原始帖子中未明确显示的其他答案的评论:

这实际上就是我想要做的。我正在尝试将“CV_Cover”拆分为 2 个不同的节点,一个包含 folio = 131 的页面。

考虑在其父元素books 的模板内将子元素book 一分为二,然后进行相应处理。

<?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

【讨论】:

  • 谢谢,我会尽快尝试并通知您 ;-)
  • 谢谢 Parfait,这正是我需要的。真的真的很感激:-)
  • 很高兴听到并乐于提供帮助!编码愉快!
【解决方案2】:

输入 xml 包含页面 1、2、131 和 132
第一个复制语句中的选择器匹配小于或等于 2 的页面,
所以它将返回第 1 页和第 2 页
第二个复制语句中的选择器匹配大于或等于 131 的页面,
所以它会返回第 131 和 132 页

【讨论】:

  • 嗨@crowne,这实际上就是我想要做的。我正在尝试将 "CV_Cover" 拆分为 2 个不同的 节点,一个包含 folio = 131 的页面。现在结果非常好,我在 Pages 和 Page 节点之间只有一个 标签。
  • 我只是注意到我没有使用我在原始问题中引用的节点的反向 tik,所以我现在就对其进行了调整。了解我的问题会更清楚
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多