Home > Software engineering >  XSL 1.0 choose in a for each loop with true/false value
XSL 1.0 choose in a for each loop with true/false value

Time:06-09

Input XML:

        <?xml version="1.0" encoding="UTF-8"?><MetrixXML xmlns="http://www.lithotechnics.com" DocumentVersion="Metrix" SchemaVersion="1.0" Units="Millimeters">
          <ResourcePool>
            <RGBColor Blue="167" Green="201" ID="Ref_1" Red="108"/>
            <Stock Grade="1" ID="Ref_3" Name="" Thickness="0.25" Vendor="" Weight="135" WeightUnit="gsm"/>
            <FoldingScheme ID="Ref_4" JDFFoldCatalog="F8-7"/>
            <Ink ID="Ref_5" Name="Cyan" Type="ProcessCyan">
              <CMYKColorRef rRef="Ref_6"/>
            </Ink>
            <CMYKColor Black="0" Cyan="100" ID="Ref_6" Magenta="0" Yellow="0"/>
            <Ink ID="Ref_7" Name="Magenta" Type="ProcessMagenta">
              <CMYKColorRef rRef="Ref_8"/>
            </Ink>
            <CMYKColor Black="0" Cyan="0" ID="Ref_8" Magenta="100" Yellow="0"/>
            <Ink ID="Ref_9" Name="Yellow" Type="ProcessYellow">
              <CMYKColorRef rRef="Ref_10"/>
            </Ink>
            <CMYKColor Black="0" Cyan="0" ID="Ref_10" Magenta="0" Yellow="100"/>
            <Ink ID="Ref_11" Name="Black" Type="ProcessBlack">
              <CMYKColorRef rRef="Ref_12"/>
            </Ink>
            <CMYKColor Black="100" Cyan="0" ID="Ref_12" Magenta="0" Yellow="0"/>
            <BindingMachine DeviceID="" ID="Ref_13"/>
            <RGBColor Blue="5" Green="218" ID="Ref_15" Red="98"/>
            <FoldingScheme ID="Ref_17" JDFFoldCatalog="F4-1"/>
            <BindingMachine DeviceID="" ID="Ref_23"/>
            <RGBColor Blue="74" Green="103" ID="Ref_25" Red="110"/>
            <RGBColor Blue="10" Green="90" ID="Ref_31" Red="109"/>
            <RGBColor Blue="239" Green="215" ID="Ref_38" Red="121"/>
            <FoldingScheme ID="Ref_40" JDFFoldCatalog="F12-7"/>
            <RGBColor Blue="211" Green="37" ID="Ref_42" Red="57"/>
            <RGBColor Blue="115" Green="85" ID="Ref_45" Red="59"/>
            <RGBColor Blue="198" Green="63" ID="Ref_55" Red="14"/>
          </ResourcePool>
  <AuditPool>
    <Audit AgentName="APSS" AgentVersion="" Event="Created" TimeStamp="2022-06-06T07:43:55.159432Z"/>
  </AuditPool>
  <Project AutoNumberOut="True" Description="" Name="" Notes="" ProjectID="">
    <ProductPool>
      <Product DueDate="2022-06-07T09:00:00" FinishedTrimHeight="210.0" FinishedTrimWidth="97.5" ID="Ref_0" Name="DE221357184X01" Notes="2500 | ON | 135ma | 4/4 | F-ZBK-8S | BNDLI | SaddleStitch | Links" RequiredQuantity="2500" TextContentFile="" Type="Bound">
        <DisplayColor>
          <RGBColorRef rRef="Ref_1"/>
        </DisplayColor>
        <ComponentPool>
          <Component Active="true" ChildIndex="-1" Cover="false" ID="Ref_2" MIS_ID="DE221357184X01FS01" Priority="1">
            <StockRef rRef="Ref_3"/>
            <FoldingSchemeRef rRef="Ref_4"/>
          </Component>
        </ComponentPool>
        <PagePool>
          <PageDefaults/>
          <Page BleedBottom="2.5" BleedLeft="0.0" BleedRight="2.5" BleedTop="2.5" Number="1" Rotation="0">
            <InkRef rRef="Ref_5"/>
            <InkRef rRef="Ref_7"/>
            <InkRef rRef="Ref_9"/>
            <InkRef rRef="Ref_11"/>
          </Page>
          <Page BleedBottom="2.5" BleedLeft="2.5" BleedRight="0.0" BleedTop="2.5" Number="2" Rotation="0">
            <InkRef rRef="Ref_5"/>
            <InkRef rRef="Ref_7"/>
            <InkRef rRef="Ref_9"/>
            <InkRef rRef="Ref_11"/>
          </Page>
          <Page BleedBottom="2.5" BleedLeft="0.0" BleedRight="2.5" BleedTop="2.5" Number="3" Rotation="0">
            <InkRef rRef="Ref_5"/>
            <InkRef rRef="Ref_7"/>
            <InkRef rRef="Ref_9"/>
            <InkRef rRef="Ref_11"/>
          </Page>
          <Page BleedBottom="2.5" BleedLeft="2.5" BleedRight="0.0" BleedTop="2.5" Number="4" Rotation="0">
            <InkRef rRef="Ref_5"/>
            <InkRef rRef="Ref_7"/>
            <InkRef rRef="Ref_9"/>
            <InkRef rRef="Ref_11"/>
          </Page>
          <Page BleedBottom="2.5" BleedLeft="0.0" BleedRight="2.5" BleedTop="2.5" Number="5" Rotation="0">
            <InkRef rRef="Ref_5"/>
            <InkRef rRef="Ref_7"/>
            <InkRef rRef="Ref_9"/>
            <InkRef rRef="Ref_11"/>
          </Page>
          <Page BleedBottom="2.5" BleedLeft="2.5" BleedRight="0.0" BleedTop="2.5" Number="6" Rotation="0">
            <InkRef rRef="Ref_5"/>
            <InkRef rRef="Ref_7"/>
            <InkRef rRef="Ref_9"/>
            <InkRef rRef="Ref_11"/>
          </Page>
          <Page BleedBottom="2.5" BleedLeft="0.0" BleedRight="2.5" BleedTop="2.5" Number="7" Rotation="0">
            <InkRef rRef="Ref_5"/>
            <InkRef rRef="Ref_7"/>
            <InkRef rRef="Ref_9"/>
            <InkRef rRef="Ref_11"/>
          </Page>
          <Page BleedBottom="2.5" BleedLeft="2.5" BleedRight="0.0" BleedTop="2.5" Number="8" Rotation="0">
            <InkRef rRef="Ref_5"/>
            <InkRef rRef="Ref_7"/>
            <InkRef rRef="Ref_9"/>
            <InkRef rRef="Ref_11"/>
          </Page>
        </PagePool>
        <BindingMachineRef rRef="Ref_13"/>

XSL:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:l="http://www.lithotechnics.com" exclude-result-prefixes="l">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/">
        <!-- Root -->
        <xsl:element name="Product">
            <xsl:attribute name="ID">
                <xsl:value-of select="//l:Project/@ProjectID"/>
            </xsl:attribute>
            <xsl:attribute name="Version">1.0</xsl:attribute>
            <xsl:attribute name="DueDate"></xsl:attribute>
            <xsl:attribute name="Copies"></xsl:attribute>
            <xsl:attribute name="ProductType">Gang</xsl:attribute>
        </xsl:element>           
                    <xsl:choose>
                    <xsl:variable name="bound" select="l:Resourcepool/l:BindingMachine"/>
                    <xsl:when test="$bound = 1">
                        <xsl:for-each select="//l:Product">
                            <xsl:element name="BoundComponent"/>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:for-each select="//l:Product">
                            <xsl:element name="UnboundComponent"/>
                        </xsl:for-each>
                    </xsl:otherwise>
                    </xsl:choose>

Output:

    <?xml version="1.0" encoding="utf-8"?>
<Product ID="68174834" Version="1.0" DueDate="" Copies="" ProductType="Gang">
   <UnboundComponent ID="" DueDate="2022-04-14T12:00:00" Priority="Normal" Bleed="" Group="" Copies="100" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-14T12:00:00" Priority="Normal" Bleed="" Group="" Copies="100" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-12T12:00:00" Priority="Normal" Bleed="" Group="" Copies="50" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-12T12:00:00" Priority="Normal" Bleed="" Group="" Copies="100" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-14T12:00:00" Priority="Normal" Bleed="" Group="" Copies="50" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-14T12:00:00" Priority="Normal" Bleed="" Group="" Copies="100" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-14T12:00:00" Priority="Normal" Bleed="" Group="" Copies="50" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-14T12:00:00" Priority="Normal" Bleed="" Group="" Copies="50" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-14T12:00:00" Priority="Normal" Bleed="" Group="" Copies="50" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-14T12:00:00" Priority="Normal" Bleed="" Group="" Copies="100" Material="90ma"/>
   <Source src=""/>
   <UnboundComponent ID="" DueDate="2022-04-19T12:00:00" Priority="Normal" Bleed="" Group="" Copies="50" Material="90ma"/>
   <Source src=""/>
</Product>

Question:

So what i'm trying to do is have a choose that checks if the element "BindingMachine" Exists and if so, i want the xsl to name the Product Elements "BoundComponent" if not they should be named "UnboundComponent". I'm not sure if this is the best way to do it or if it would be easier with an if case, but no matter what i tried so far i couldn't get it to work unfortunatly, any ideas?

CodePudding user response:

I am guessing you want to do something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:l="http://www.lithotechnics.com" 
exclude-result-prefixes="l">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/l:MetrixXML">
    <xsl:variable name="elem-name">
        <xsl:choose>
            <xsl:when test="l:ResourcePool/l:BindingMachine">BoundComponent</xsl:when>
            <xsl:otherwise>UnboundComponent</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <!-- output -->
    <Product ID="{//l:Project/@ProjectID}" Version="1.0" DueDate="" Copies="" ProductType="Gang">
        <xsl:for-each select="//l:Product">
            <xsl:element name="{$elem-name}">
                <!-- some code here? -->
            </xsl:element>
        </xsl:for-each>
    </Product>
</xsl:template>

</xsl:stylesheet>
  • Related