0

I would like to ask in debugging in the following XSLT code. My XSLT checks for images in a directory then creates an element based on this image if exists.

XSLT:

<xsl:stylesheet exclude-result-prefixes="xs fs" version="2.0" xmlns:fs="java.io.File" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes" method="xml"/>

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

  <!-- remove not available elements -->
  <xsl:template match="xml">
    <xsl:copy>
        <xsl:for-each select="product[avail != 'No']">
          <xsl:copy>
            <xsl:copy-of select="@*|node()"/> 
          </xsl:copy>
      </xsl:for-each>
    </xsl:copy>        
  </xsl:template>

  <!-- add extra images if exists $code_001.jpg, _002.jpg... -->
  <!--example: <IMAGE1>test.com/0012307_001.jpg</IMAGE> -->
  <!-- ls /home/hpapagaj/images/: 001230.jpg, 0012307_001.jpg etc. -->

  <xsl:template match="product">
    <xsl:copy>
    <xsl:apply-templates />
    <xsl:variable name="imageproductid" select="code" />

    <xsl:for-each select="1 to 5">
        <xsl:variable name="filename"
             select="concat('/home/hpapagaj/images/',$imageproductid,'_00', ., '.jpg')" />
        <xsl:if test="fs:exists(fs:new($filename))">
            <xsl:element name="{concat('IMAGE', .)}">
                <xsl:value-of select="concat('https://test.com/',tokenize($filename, '/')[last()])" />
            </xsl:element>
        </xsl:if>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

My XML is simple as:

<xml>
     <product>
      <code>001237</code>
      <avail>Yes</avail>
      <IMGURL>https://test.com/001237.jpg</IMGURL>
   </product>
</xml>

Since I added filtering (remove not available elements):

  <!-- remove not available elements -->
  <xsl:template match="xml">
    <xsl:copy>
        <xsl:for-each select="product[avail != 'No']">
          <xsl:copy>
            <xsl:copy-of select="@*|node()"/> 
          </xsl:copy>
      </xsl:for-each>
    </xsl:copy>        
  </xsl:template>

...it stopped working. I can move this part to another XSLT, but I would like to transform in one step.

1 Answer 1

1

If you have a template for product elements doing the right transformation jobs but want to exclude some product elements from the transformation and instead have them removed in the transformation result then add an empty template

<xsl:template match="product[avail = 'No']"/>

For the xml element you don't need any particular template as the identity transformation template you have first takes care of it and together with the above empty templates ensures that the not available products are not copied to the output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.